I have been using this site as an example of how to make a dynamic form in Django. In his view he uses
if request.method == 'POST':
form = UserCreationForm(request.POST)
to pass the data into the form and in the form constructor he uses
extra = kwargs.pop('extra')
to access the POST data. I tried to do something similar with my view:
def custom_report(request):
if request.method=='POST':
form=CustomQueryConstraintForm(request.POST)
else:
form=CustomQueryConstraintForm()
return render(request, 'frontend/custom_report.html', {'form':form})
In my form constructor I printed args and kwargs and found that kwargs is empty and args is a tuple containing the QueryDict that in turn contains the POST data. If I try instead to use form=CustomQueryConstraintForm(**request.POST), each element in kwargs is a list containing the value of the field as its only element. Am I doing something wrong here? If not, is there a more elegant way of accessing the data than args[0][element_name][0]?
That is expected behavior for forms: the POST data you pass into the form is the first argument, args[0] and not a keyword argument. What are you looking for?
data = args[0]
print data['my_field']
and in the form constructor he uses
extra = kwargs.pop('extra') to access
the POST data.
kwargs.pop('extra') is not getting POST data. It is a list of questions associated with that given user -- some scenario given by the author that the "marketing department" handed you.
In any case, if you need to access the post data at any point in a form, I find self.data the cleanest which is set in forms.__init__.
class MyForm(forms.Form):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.data['my_field']
If I understand correctltly, after a POST request you are trying to redisplay the same form page in which the form fields are filled, right? If yes, this is what you need:
form = CustomQueryConstraintForm(initial=request.POST)
You can also access the form data through the cleaned_data dictionary after using the is_valid() method. Like this:
jobForm = JobForm(request.POST)
jobForm.is_valid()
jobForm.cleaned_data
it's a dictionary of the values that have been entered into the form.
Related
Let's say I have a Django form with ChoiceField:
class MyForm(forms.Form):
name = forms.CharField(label="Name", required=True)
some_object = forms.ChoiceField(label="Object", choices=[(0, '-----')] + [(x.id, x.name) for x in Obj.objects.all()])
Choisefield is being initialized with list of objects headed by 'empty choice'. There is no object with pk=0.
In that form I have a clean() method:
def clean(self):
if not Obj.objects.filter(self.cleaned.data['some_object'].exists():
self.errors.update({'some_object': ['Invalid choice']})
It works well when I'm sending a form to a server, if data in it doesn't match conditions, field.is_valid returns False and I render form with error messages. But, when I create an empty form like this:
if request.method == 'GET':
data = {'name': 'Untitled',
'some_object': 0}
form = MyForm(data)
return render(request, 'app\template.html', {'form': form})
Django renders form with error message ('Invalid choice') even though form was just created and 'Object' select was intended to be set in empty position. Is it possible to disable form clean() method in specific cases? Or maybe I'm doing this all wrong? What is the best practice to create an empty form?
The problem is that a form with any data dictionary passed to it counts as a "bound" form, against which Django will perform data validation. See here for details: https://docs.djangoproject.com/en/2.1/ref/forms/api/#bound-and-unbound-forms
You want an "unbound" form - to have default initial values in here, just set the initial property on your form fields. See full details here: https://docs.djangoproject.com/en/2.1/ref/forms/fields/#initial
I'm having big trouble understanding the whole forms business in django. As I understand it the cleaned form data is a dictionary. So all my defined form fields should be in the dictionary like so: {'definedform': userinput, ...}. Is this correct?
I want to create a form in which a user can input data. This data should then be send to a different view, in which the inputted data is rendered with a latex template (and subsequently rendered into a pdf). This works more or less fine if I define the context in the /create_pdf/ view and grab the user input manually. But I suppose there is a nicer way. What I think should work:
def index(request):
if request.method == "POST":
persoform = PersonalForm(request.POST, prefix='personal')
if persoform.is_valid():
content = persoform.cleaned_data()
content = Context(content)
return HttpResponseRedirect('/create_pdf/')
else:
persoform = PersonalForm()
return render(request, 'app/template.html', {'persoform': persoform})
And in my /create_pdf/ view:
def create_pdf(request):
template = get_template('app/latextemplate.tex')
rendered_tpl = template.render(content)
[...]
So, how can I make sure, to pass the data from my index view to my create_pdf view?
EDIT:
Forgot to mention: The error is "'content' not defined". So I understand that the /create_pdf/ view doesn't get content dictionary, but I have no idea how I would make sure that it does.
Put the data in to the session on submit, and pop it out in the second view.
if form.is_valid():
request.session['perso'] = form.cleaned_data
return HttpResponseRedirect('/create_pdf/')
...
def create_pdf(request):
data = request.session.pop('perso'], {})
I've been scouring StackOverflow, but I haven't found an answer to this that works for me. I am relatively new to Python and Django, so maybe I'm thinking about it wrong.
To make a simple example, imagine two views with different associated URLs. This is not supposed to be perfect code. I'm just trying to figure out how to get a variable-length list of items from view 1 into view 2. I don't see a way to do it via the URL because the list may be variably long. Shouldn't this be extremely easy to do?
def view2(request, list_to_process):
use list_to_process to manufacture formset (e.g. make a formset with one entry for each item in the list)
return render(request, 'Project/template2.html', {'formset': formset})
def view1(request):
if request.method == "POST":
if form.is_valid():
result = form.cleaned_data
list_to_process = []
for item in result:
list_to_process.append(item)
*WHAT CODE DO I USE HERE TO CALL VIEW2 AND SEND IT list_to_process AS AN ARGUMENT OR REQUEST ADDITION?*
else:
formset = formsettype()
helper = AssayHelper() (defined elsewhere)
helper.add_input(Submit("submit", "Submit")
return render(request, 'Project/template1.html', {'formset': formset, 'helper': helper})
Can someone please help? Thanks.
That is exactly what the session is for. In view 1:
request.session['list'] = list_to_process
And in view 2:
list_to_process = request.session['list']
If you are willing to use session then go with the answer given by #Daniel,
But in your case it seems that you are not going on separate url, you just need to render it in the same url but need the output from that view, in that case take help from named paramter of python functions like this -
def view2(request, list_to_process=None, **kwargs):
use list_to_process to manufacture formset (e.g. make a formset with one entry for each item in the list)
return render(request, 'Project/template2.html', {'formset': formset})
def view1(request):
if request.method == "POST":
if form.is_valid():
result = form.cleaned_data
list_to_process = []
for item in result:
list_to_process.append(item)
return view2(request, list_to_process=list_to_process)
else:
.....
The benefit of using named parameter is that, they are optional and thus will not throw error if they are not provided, for example, when that view is called directly instead from inside view1
I am writing a change password form, and I would like to pass the original password to the form for validation purposes, but the only way to do that is to make it a hidden field and pass it in with the rest of the data. I obviously don't want to do that, I'd rather just pass it to the form constructor from within the view, but if it's not passed in with the formdata then that won't work.
class MyForm(Form):
...
original_password = HiddenField()
...
def validate_current_password(form, field):
if field.data != form.original_password.data:
ERROR
form = MyForm(request.POST, original_password=password) Does not work unless request.POST is empty, or unless I actually render and submit the original password with the form. form.original_password.data is empty otherwise.
For those of you familiar with formencode's "state" variable, I solved this by subclassing Form and adding a state variable with information to assist in validation.
I have some question:
I use django form, and fields like MultipleChoiceField
in view.py I clean data and get GET URL like this
http://localhost:8000/?category=&style=&sex=&brand=ASICS&brand=Be+Positive&low_price=&high_price=
Give me advise, can I regroup brand field and hide empty.
I want getting something like this:
http://localhost:8000/?brand=1+2
And else one question:
How can I set empty value(empty_label) for forms.ModelMultipleChoiceFIeld
forms.py:
brand = forms.MultipleChoiceField(required=False,
widget=forms.SelectMultiple(attrs={'size':1})
)
def __init__(self,app_label=None, *args, **kwargs):
super(Search, self).__init__(*args, **kwargs)
self.fields['brand'].choices = [('', 'All brands')]+[(brand.name, brand) for brand in Brand.objects.all() ]
views.py:
if request.method == 'GET' and request.GET:
form = SearchForm(app_label, request.GET)
if form.is_valid():
brands = form.cleaned_data['brand']
kwargs.update({"brand__name__in": brands})
This is how the browser submits multiple data. It's part of the HTML specification, trying to change it would be folly and technically I can't understand why you would try to care about how your url GET data looks.
That being said, if you want to change the way it submits you'll need javascript to transform the data on form submit. Django has nothing to do with the matter.
Using jQuery for example:
$('#form').submit(function(){
//Get form data
//Transform into my custom set of vars
//Redirect to form's ACTION with my querystring appended.
});
Please keep in mind you will not get any automatic parsing of the values on the Django side. Normally it would turn it into a list for you, but now you're responsible for parsing the 'value+value+value' yourself.
For empty label in forms you could do this -
class SomeForm(forms.Form):
h=forms.CharField(label=u'',widget=forms.TextInput(attrs={'value':'Search'}))
By keeping label as '', you get the label as empty. The attrs are basically the HTML attributes of the form text field.
UPDATE: I didn't understand the first part of your Q, elaborate...