Search⌘ K

Collecting Data via Django Form

Explore how to capture and process data from Django forms by handling POST requests. Understand form validation, extracting specific data fields, and rendering data in templates. This lesson guides you through practical code examples to manage form input effectively in Django.

Getting data from the Django form

Let’s see how we can grab data from our Django form.

Step 1: Grab data from the form

We need to write some code so we can grab data from our POST request. Let’s see the code first, and then break it down line by line:

Python 3.5
def forms(request):
form = TestForm( request.POST or None)
data = "None"
text= "None"
if form.is_valid():
data = form.cleaned_data
text = form.cleaned_data.get("text")
return render(request,'first_app/forms.html', {'form': form,'data': data, 'text': text})
  • At line #1, we are passing request as a parameter. The request object holds the information regarding the form.

  • At line #2 ...