Django form


HTML forms are the classic way of website interactivity. This chapter will introduce how to use Django to process form data submitted by users.


HTTP request

The HTTP protocol works in a "request-reply" manner. When a client sends a request, they can append 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 created a search.py ​​file in the previous project to receive user requests:

# -*- coding: utf-8 -*-

from django.http import HttpResponse
from django.shortcuts import render_to_response

# 表单
def search_form(request):
	return render_to_response('search_form.html')

# 接收请求数据
def search(request):  
	request.encoding='utf-8'
	if 'q' in request.GET:
		message = '你搜索的内容为: ' + request.GET['q'].encode('utf-8')
	else:
		message = '你提交了空表单'
	return HttpResponse(message)

Add search_form.html in the template directory template Form:

<html>
<head>
	<meta charset="utf-8" /> 
    <title>Search - w3cschool.cc</title>
</head>
<body>
    <form action="/search/" method="get">
        <input type="text" name="q">
        <input type="submit" value="Search">
    </form>
</body>
</html>

urls.py The rules are modified to the following form:

from django.conf.urls import *
from HelloWorld.view import hello
from HelloWorld.testdb import testdb
from HelloWorld import search

urlpatterns = patterns("",
	('^hello/$', hello),
	('^testdb/$', testdb),
	(r'^search-form/$', search.search_form),
	(r'^search/$', search.search),
)

Access address: http://192.168.45.3:8000/search-form/ and search, the results are as follows:

1044.jpg

1045.jpg

POST method

We used the GET method above. View display and request processing are divided into two functions.

The POST method is more commonly used when submitting data. We use this method below and use a URL and processing function to display the view and handle the request at the same time.

We create post.html in tmplate:

<html>
<head>
	<meta charset="utf-8" /> 
    <title>Search - w3cschool.cc</title>
</head>
<body>
	<form action="/search-post/" method="post">
		{% csrf_token %}
		<input type="text" name="q">
		<input type="submit" value="Submit">
	</form>

	<p>{{ rlt }}</p>
</body>
</html>

At the end of the template, we add an rlt mark to reserve a place for the table processing results.

There is also a {% csrf_token %} tag behind the form. The full name of csrf is Cross Site Request Forgery. This is a feature provided by Django to prevent disguised submission requests. Forms submitted using the POST method must have this tag.

Create a new search2.py file in the HelloWorld directory and use the search_post function to process the POST request:

# -*- coding: utf-8 -*-

from django.shortcuts import render
from django.core.context_processors import csrf

# 接收POST请求数据
def search_post(request):
	ctx ={}
	ctx.update(csrf(request))
	if request.POST:
		ctx['rlt'] = request.POST['q']
	return render(request, "post.html", ctx)

urls.py The rules are modified to the following form:

from django.conf.urls import *
from HelloWorld.view import hello
from HelloWorld.testdb import testdb
from HelloWorld import search
from HelloWorld import search2

urlpatterns = patterns("",
	('^hello/$', hello),
	('^testdb/$', testdb),
	(r'^search-form/$', search.search_form),
	(r'^search/$', search.search),
	(r'^search-post/$', search2.search_post),
)

Access http: //192.168.45.3:8000/search-post/ The displayed results are as follows:

1046.jpg

After completing the above example, our directory structure is:

HelloWorld
|-- HelloWorld
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- models.pyc
|   |-- search.py
|   |-- search.pyc
|   |-- search2.py
|   |-- search2.pyc
|   |-- settings.py
|   |-- settings.pyc
|   |-- testdb.py
|   |-- testdb.pyc
|   |-- urls.py
|   |-- urls.pyc
|   |-- view.py
|   |-- view.pyc
|   |-- wsgi.py
|   `-- wsgi.pyc
|-- TestModel
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- admin.py
|   |-- models.py
|   |-- models.pyc
|   |-- tests.py
|   `-- views.py
|-- manage.py
`-- templates
    |-- base.html
    |-- hello.html
    |-- post.html
    `-- search_form.html

3 directories, 29 files

Request object

The first parameter of each view function is an HttpRequest object, like the following hello() function:

from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello world")

The HttpRequest object contains some information about the current request URL:

Properties

Description

path

#The full path to the requested page, excluding the domain name—for example, "/hello/".

method

String representation of the HTTP method used in the request. Expressed in all caps. 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 QueryDict documentation.

POST

Dictionary-like object containing all HTTP POST parameters. See QueryDict documentation.

It is also possible that the server receives an empty POST request. In other words, the form submits the request through the HTTP POST method, but there is no data in the form. Therefore, you cannot use the statement if request.POST to determine whether to use the HTTP POST method; instead, use if request.method == "POST" (see the method attribute in this table).

Note: POST does not include file-upload information. See FILES attribute.

REQUEST

For convenience, this attribute is a collection of POST and GET attributes, but it has special characteristics. Look for POST attributes first, then GET attributes. Learn from PHP's $_REQUEST.

For example, if GET = {"name": "john"} and POST = {"age": '34'}, then the value of REQUEST["name"] is "john", REQUEST[" The value of age"] is "34".

It is strongly recommended to use GET and POST, because these two attributes are more explicit and the code written is easier to understand.

COOKIES

A standard Python dictionary object containing all cookies. Keys and values ​​are both strings. See Chapter 12 for a more detailed explanation of cookies.

FILES

Dictionary-like object containing all uploaded files. Each Key in FILES is the value of the name attribute in the <input type="file" name="" /> tag. Each value in FILES is also a standard Python dictionary object, containing the following three Keys:

  • filename: Upload file name, expressed as a Python string

  • content-type: Content type of the uploaded file

  • content: The original content of the uploaded file

Note: Only when the request method is POST, and the <form> in the request page has enctype="multipart/form- data" attribute, FILES only has data. Otherwise, FILES is an empty dictionary.

META

A dictionary containing all available HTTP headers. For example:

  • CONTENT_LENGTH

  • CONTENT_TYPE

  • QUERY_STRING: Unparsed raw query string

  • REMOTE_ADDR: Client IP address

  • REMOTE_HOST: Client host name

  • SERVER_NAME: Server Host name

  • SERVER_PORT: Server port

These headers in META are prefixed with HTTP_ as the key, for example:

  • HTTP_ACCEPT_ENCODING

  • HTTP_ACCEPT_LANGUAGE

  • HTTP_HOST: HTTP host header information sent by the client

  • HTTP_REFERER: referring page

  • HTTP_USER_AGENT: client’s user-agent string

  • HTTP_X_BENDER: X-Bender header information

user

is a django.contrib.auth.models.User object, representing the current login User.

If the access user is not currently logged in, user will be initialized as an instance of django.contrib.auth.models.AnonymousUser.

You can identify whether the user is logged in through the user's is_authenticated() method:


if request.user.is_authenticated():
    # Do something for logged-in users.
else:
    # Do something for anonymous users.

This attribute is only available when the AuthenticationMiddleware in Django is activated

session

The only readable and writable attribute represents the dictionary object of the current session. This property is only available when session support in Django is activated. See Chapter 12.

raw_post_data

Raw HTTP POST data, not parsed. Useful for advanced processing.

The Request object also has some useful methods:

MethodDescription
__getitem__(key)Return the key value of GET/POST, first take POST, then take GET. Throws KeyError if the key does not exist.
This is where we can access the HttpRequest object using dictionary syntax.
For example, request["foo"] is equivalent to request.POST["foo"] and then request.GET["foo"].
has_key()Check whether request.GET or request.POST contains the Key specified by the parameter.
get_full_path()Returns the request path containing the query string. For example, "/music/bands/the_beatles/?print=true"
is_secure()Returns True if the request is secure, that is, issued It's an HTTPS request.

QueryDict Object

In the HttpRequest object, the GET and POST attributes are instances of the django.http.QueryDict class.

QueryDict is a dictionary-like custom class used to handle situations where a single key corresponds to multiple values.

QueryDict implements all standard dictionary methods. Also includes some unique methods:

MethodDescription

__getitem__

is a little different from standard dictionary processing, that is, if Key corresponds to multiple Values, __getitem__() returns the last value.

__setitem__

Set the value list of the key specified by the parameter (a Python list). Note: It can only be called on a mutable QueryDict object (that is, a copy of a QueryDict object generated by copy()).

get()

If key corresponds to multiple values, get() returns the last value.

update()

The parameter can be a QueryDict or a standard dictionary. Unlike the standard dictionary update method, this method adds dictionary items rather than replacing them:

>>> q = QueryDict('a=1')

>>> q = q.copy() # to make it mutable

>>> q.update({'a': '2'})

>>> q.getlist('a')

 ['1', '2']

>>> q['a'] # returns the last

['2']

items()

It is a little different from the items() method of the standard dictionary. This method uses single-value logic __getitem__():

>>> q = QueryDict('a=1&a=2&a=3')

>>> q.items()

[('a', '3')]

values()

is a little different from the values() method of the standard dictionary. This method uses single-value logic __getitem__():

In addition, QueryDict also has some methods, as shown in the following table:

MethodDescription

copy()

Returns a copy of the object. The internal implementation uses copy.deepcopy() of the Python standard library. The copy is mutable—that is, the value of the copy can be changed.

getlist(key)

Returns all values ​​corresponding to the parameter key as a Python list. If the key does not exist, an empty list is returned. It's guaranteed to return a list of some sort..

setlist(key,list_)

Set key The value is list_ (unlike __setitem__()).

appendlist(key,item)

Add item to and The internal list associated with key.

setlistdefault(key,list)

is a little different from setdefault, it accepts list instead of a single value as parameter.

lists()

is a little different from items(), it will return all the values ​​of the key as A list, for example:

>>> q = QueryDict('a=1&a=2&a=3')

>>> q.lists()

[('a', ['1', '2', '3'])]

urlencode()

Returns a query string formatted string (e.g., "a=2&b=3&b=5").