Django Forms
HTML forms are a classic way for websites to be interactive. This chapter introduces how to process form data submitted by users with Django.
HTTP request
The HTTP protocol works in a "request-response" manner. When a client sends a request, it can attach data to the request. By parsing the request, the server can obtain the data sent by the client and provide specific services based on the URL.
GET Method
We create a search.py file in the previous project to receive user requests:
/HelloWorld/HelloWorld/search.py file code:
from
django
.
http
import
HttpResponse
from
django
.
shortcuts
import
render
The form
def
search_form
(
request
)
:
return
render
(
request
,
'
search_form.html
'
)
Receive request data
def
search
(
request
)
:
request
.
encoding
=
'
utf-8
'
if
'
q
'
in
request
.
GET
and
request
.
GET
[
'
q
'
]
:
message
=
'
What you searched for is:
'
request
.
GET
[
'
q
'
]
else
:
message
=
'
You submitted an empty form
'
return
HttpResponse
(
message
)
Add a search_form.html form in the templates directory:
/HelloWorld/templates/search_form.html file code:
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>Runoob Tutorial(runoob.com)
title
head
<body>
<form action="/search/" method="get">
<input type="text" name="q">
<input type="submit" value="Search" form body html>Modify the urls.py rules to the following form:
/HelloWorld/HelloWorld/urls.py file code:
from
django
.
conf
.
urls
import
url
from
.
import
views
,
testdb
,
search
urlpatterns
=
[
url
(
r
'
^hello/$
'
,
views
.
runoob
)
,
url
(
r
'
^testdb/$
'
,
testdb
.
testdb
)
,
url
(
r
'
^search-form/$
'
,
search
.
search_form
)
,
url
(
r
'
^search/$
'
,
search
.
search
)
,
]
Address accessed http://127.0.0.1:8000/search-form/ and search; the result is as follows:

POST Method
Above we used the GET method, with view display and request handling split into two functions.
The POST method is more commonly used when submitting data. We use this method below, with one URL and one handler function that both displays the view and handles the request.
We create post.html in templates:
/HelloWorld/templates/post.html file code:
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>Runoob Tutorial(runoob.com)
title
head
<body>
<form action="/search-post/" method="post">{% csrf_token %}
<input type="text" name="q">
<input type="submit" value="Search" form>
<p>{{ rlt }}
p
body
html
At the end of the template, we add an rlt marker to reserve a place for the table processing result.
After the form there is also a {% csrf_token %} tag. csrf stands for Cross Site Request Forgery. This is a feature provided by Django to prevent forged submitted requests. Forms submitted with the POST method must have this tag.
Create a new search2.py file in the HelloWorld directory and use the search_post function to handle POST requests:
/HelloWorld/HelloWorld/search2.py file code:
-- coding: utf-8 --
from
django
.
shortcuts
import
render
from
django
.
views
.
decorators
import
csrf
Receive POST request data
def
search_post
(
request
)
:
ctx
={}
if
request
.
POST
:
ctx
[
'
rlt
'
]
=
request
.
POST
[
'
q
'
]
return
render
(
request
,
"
post.html
"
,
ctx
)
Modify the urls.py rules to the following form:
/HelloWorld/HelloWorld/urls.py file code:
from
django
.
conf
.
urls
import
url
from
.
import
views
,
testdb
,
search
,
search2
urlpatterns
=
[
url
(
r
'
^hello/$
'
,
views
.
hello
)
,
url
(
r
'
^testdb/$
'
,
testdb
.
testdb
)
,
url
(
r
'
^search-form/$
'
,
search
.
search_form
)
,
url
(
r
'
^search/$
'
,
search
.
search
)
,
url
(
r
'
^search-post/$
'
,
search2
.
search_post
)
,
]
Access the http://127.0.0.1:8000/search-post/ The display result is as follows:
After completing the above examples, our directory structure is:
HelloWorld
|-- HelloWorld
| |-- __init__.py
| |-- __init__.pyc
| |-- search.py
| |-- search.pyc
| |-- search2.py
| |-- search2.pyc
| |-- settings.py
| |-- settings.pyc
| |-- testdb.py
| |-- testdb.pyc
| |-- urls.py
| |-- urls.pyc
| |-- views.py
| |-- views.pyc
| |-- wsgi.py
| `-- wsgi.pyc
|-- TestModel
| |-- __init__.py
| |-- __init__.pyc
| |-- admin.py
| |-- admin.pyc
| |-- apps.py
| |-- migrations
| | |-- 0001_initial.py
| | |-- 0001_initial.pyc
| | |-- __init__.py
| | `-- __init__.pyc
| |-- models.py
| |-- models.pyc
| |-- tests.py
| `-- views.py
|-- db.sqlite3
|-- manage.py
`-- templates
|-- base.html
|-- hello.html
|-- post.html
`-- search_form.html
Request Object
The first parameter of every view function is an HttpRequest object, just like the following runoob() function:
from django.http import HttpResponse
def runoob(request):
return HttpResponse("Hello world")
The HttpRequest object contains some information about the current request URL:
|
property |
Description |
|---|---|
|
path |
The full path of the requested page, excluding the domain name - for example, "/hello/". |
|
method |
A string representation of the HTTP method used in the request. In all uppercase. For example: if request.method == 'GET': |
do_something()
elif request.method == 'POST':
do_something_else() | |
GET
|A dictionary-like object containing all HTTP GET parameters. See the QueryDict documentation.
| |POST
|A dictionary-like object containing all HTTP POST parameters. See the QueryDict documentation.
It is also possible for the server to receive an empty POST request. That is, a form submits a request via the HTTP POST method, but the form may contain no data. Therefore, you cannot use the statement if request.POST to determine whether the HTTP POST method is used; you should use if request.method == "POST" (see the method attribute in this table).
Note: POST does not include file-upload information. See the FILES attribute.
| |REQUEST
|For convenience, this attribute is a combination of the POST and GET attributes, but with a special feature: it searches the POST attribute first, then the GET attribute. Borrowed from PHP's $_REQUEST.
For example, if GET = {"name": "john"} and POST = {"age": '34'}, then the value of REQUEST["name"] is "john" and the value of REQUEST["age"] is "34".
It is strongly recommended to use GET and POST, because these two attributes are more explicit and make the code easier to understand.
| |COOKIES
|A standard Python dictionary object containing all cookies. Keys and values are both strings.
| |FILES
|A dictionary-like object containing all uploaded files. Each Key in FILES is thevalue of the name attribute in the tag. Each value in FILES is also a standard Python dictionary object containing the following three Keys:
- filename: The uploaded file name, represented as a Python string
- content-type: The Content type of the uploaded file
- content: The raw content of the uploaded file
Note: Only when the request method is POST and the request page has