My request.POST data looks like this after my Django form submission:
<QueryDict: {'form-0-country': ['19389'], 'form-0-city': ['Montreal'], 'form-0-eid': ['450'], 'form-0-company': ['Nestle'], 'form-0-dept': ['HR'], 'form-1-country': ['19390'], 'form-1-city': ['Toronto'], 'form-1-eid': ['432'], 'form-1-company': ['Nestle'], 'form-1-dept': ['Finance']}>
These are values of two forms contained in two different rows on the webpage. The values from these two rows are printed sequentially in the request.POST. Is there a way to get this data printed in the backend in a simpler way so that I don't have to loop through all this data to extract the specific fields contained in it?
For eg. something like this:
<QueryDict: {'form-0-country': ['19389','19390'], 'form-0-city': ['Montreal','Toronto'], 'form-0-eid': ['450'], 'form-0-company': ['Nestle','Nestle'], 'form-0-dept': ['HR','Finance']>
so that I can easily loop through the value (lists) in the dict above.
instead of:
<QueryDict: {'form-0-country': ['19389'], 'form-0-city': ['Montreal'], 'form-0-eid': ['450'], 'form-0-company': ['Nestle'], 'form-0-dept': ['HR'], 'form-1-country': ['19390'], 'form-1-city': ['Toronto'], 'form-1-eid': ['432'], 'form-1-company': ['Nestle'], 'form-1-dept': ['Finance']}>
CodePudding user response:
You can use cleaned_data
to extract the values from your form.
For example:
country = form.cleaned_data["country"]
city = form.cleaned_data["city"]
CodePudding user response:
You should print form instead of printing request.POST
e.g.
def your_view(request):
if request.method == 'POST':
form = YourForm(request.POST)
if form.is_valid():
print(form.cleaned_data)
...
...
...