For specifically accepting input from the site visitors,and then process and respond to the input,django provides a large range of tools and libraries.
GET and POST requests in forms
Whenevar we deal with forms ,GET and POST are the only http methods that we are going to use.
The inbuilt login form in django is returned using POST method only.In that POST method browser bundles up the data from the form,encodes it for transmission,sends it to the server and at the end will receives back it’s response.
In contrast when GET method is used,it will bundles the submitted data into a string,and uses that to compose a URL.This URL will be composed of the address where the data must be send,data keys and values.This method is used in django’s documentation also.When you search in the django documentation,it will produce a URL of the form https://docs.djangoproject.com/search/?q=forms&release=1.
GET and POST both have their own advantages,so according to that they will be used for different purposes.
POST method will be used for requests that change the state of the system,in contrast GET method will be used for requests that do not affect the state of system.
Because of the fact that GET method will bundle the submitted data into URL,it would unsuitable for a password form,because the password would appear in the URL.It is also not suitable for large quantities of data or images.
Django’s role in forms
Django takes care of handling these three different types of work when working with forms:
- It will make the data for rendering by preparing and constructing it.
- It will create HTML forms for the data
- It will take care of receiving and processing the submitted data.
Building A Form In Django
Suppose in order to obtain the user name in your website,you want to create a form.For that you will need something like this in your template.
<form action="/your-name/" method="post"> <label for="your_name">Your name: </label> <input id="your_name" type="text" name="your_name" value="{{ current_name }}"> <input type="submit" value="OK"> </form>
This section of code will tell the browser to return the form data to /your-name/ URL,using the POST method.If the template contains a current_name variable then it will be used to pre-fill the your_name variable.The POST request which is sent to the server will contain the form data,when the form is submitted.After writing this code,you will also need a view corresponding to that /your-name/ URL, which will find the appropriate key/value pairs in the request, and then process them.
The Form Class
Now we already know what our html file looks like,we will define a form class for that
from django import forms class NameForm(forms.Form): your_name = forms.CharField(label='Your name', max_length=100)
This three lines of code will define a Form class with a single field(your_name).
The whole form, when rendered for the first time, it will look like this:
<label for="your_name">Your name: </label> <input id="your_name" type="text" name="your_name" maxlength="100" required />
Now to handle the form we also need to instantiate it in the view as following:
from django.http import HttpResponseRedirect from django.shortcuts import render from .forms import NameForm def get_name(request): # if this is a POST request we need to process the form data if request.method == 'POST': # create a form instance and populate it with data from the request: # This is called binding data to the form. form = NameForm(request.POST) # check whether it's valid: if form.is_valid(): # process the data in form.cleaned_data as required # ... # redirect to a new URL: return HttpResponseRedirect('/thanks/') # if a GET (or any other method) we'll create a blank form and render that instance to template else: form = NameForm() return render(request, 'name.html', {'form': form})
After that much of work,we don’t need to do much in name.html template.We just need to add the following content,it will create a form for us.
<form action="/your-name/" method="post"> {% csrf_token %} {{ form }} <input type="submit" value="Submit" /> </form>
All the form’s fields and their attributes will be unpacked into HTML markup from that {{ form }}
by Django’s template language.
This is all about creating form and form class in django.