...

/

Collecting Data via Django Form

Collecting Data via Django Form

Let’s learn how to process data from a form 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:

Press + to interact
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 ...