Accessing POST data in Django form

Viewed 14749

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]?

3 Answers
Related