Personal tools

Contact Us 24/7 > 1 866.SIX FEET

Skip to content. | Skip to navigation

Home > Blog > Django Forms: Handling form data in the POST


Banner image

Django Forms: Handling form data in the POST

written by Chrissy Wainwright on Monday February 12, 2018
Comments | Filed under:

Many forms created in Django are simply for creating content based on a model, and won't require any special processing. But some forms you create will require custom changes to be made with the data sent to the form. Here are some tips for getting that data in the view.

In a Class-Based View subclassing FormView, this can be done within a post function. If you have a function-based view, check if request.method == 'POST'.

If the view is connected to a defined Form, use the built-in function is_valid to check the form for errors. Notice you can pass form.errors as a message to display on the form, to give users feedback on why the validation failed:

form = ProfileForm(request.POST, instance=request.user)
if form.is_valid:
# process data, return next page
messages.error(request, form.errors)
# return form with entered data, display messages at the top

Next, make a copy of the form data:

data = request.POST.copy()

Then you can use data to access the user's submitted data.

  • To get data from a specific field, use what is set in the name attribute to call the field. This is not always the same as id.
  • Use .get() to access the field, because if the field is left empty in the form, it will not be in data, and None will be returned.


name = data.get('name')
age = data.get('age')

Checkboxes will get the value from the checked option, or 'on' if there is no value

married = data.get('married')

Radio buttons will return the value of the selected option

kids = data.get('no_of_kids')

For multiselects or inlines (one-to-many fields) that have multiple values, use .getlist():

hobbies = data.getlist('hobbies')

Uploaded files aren't part of the data, but access through FILES:

portrait_file = request.FILES.get('portrait')
Add comment

You can add a comment by filling out the form below. Plain text formatting.

Chrissy Wainwright
Senior Developer
Chrissy's Recent Posts:
Django Forms: Handling form data in the POST (02/12/2018)

Next Steps

Select a type of support:

Contact our sales team

First name:
Last name:
Phone Number:
Fight spam:
What is + ?
Call Us 1 866.SIX FEET