HTML form is the classic way interactive website. Processed form data submitted by the user with Django. HTTP request HTTP protocol to " request - reply " way of working. When the client sends a request, the data may be added in the request. Server by parsing the request, you can get data from the customer, and to provide specific services based on URL.
GET method Search.py create a file in the project, for receiving a user request: / The HelloWorld / the HelloWorld / search.py file code: # - * - 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') # Receiving request data DEF Search (Request): request.encoding='utf-8' if 'q' in request.GET and request.GET['q']: Message = ' what you search for: ' + `` request.GET`` [ ' Q ' ] the else : the Message = ' you have submitted an empty form ' return HttpResponse (the Message)
Add search_form.html form templates in the templates directory: / the HelloWorld / templates / search_form.html file code: <! DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>(runoob.com)</title> </head> <body> <form action="/search" method="get"> <input type="text" name="q"> <input type="submit" value="搜索"> </form> </body> </html>
urls.py rule modifying the following form: / the HelloWorld / the HelloWorld / urls.py file code: from django.conf.urls Import URL from . Import View, the testdb, Search urlpatterns = [ url(r'^hello$', view.hello), url(r'^testdb$', testdb.testdb), url(r'^search-form$', search.search_form), url(r'^search$', search.search), ]
Access address and search http://127.0.0.1:8000/search-form
POST method Above using the GET method. Display view and request processing is divided into two function processing. POST method more commonly used when submitting data. Using this method the following, and with a URL and processing functions, and the view also shows the processing request. Creating post.html in templates: / The HelloWorld / Templates / post.html file code: <! DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>(runoob.com)</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, add a rlt mark placeholder for the form processing results. A table and there { % csrf_token% } tags. csrf stands for Cross Site Request Forgery. This is a function provided by Django prevent disguised submitted the request. POST method to submit the form, you must have this label. Search2.py new file in the directory and use search_post HelloWorld function to handle a POST request: / The HelloWorld / the HelloWorld / search2.py file code: # - * - Coding: UTF-8 - * - from django.shortcuts import render from django.views.decorators import csrf # Received POST request data DEF search_post (Request): ctx ={} if request.POST: ctx['rlt'] = request.POST['q'] return render(request, "post.html", ctx)
# -*- coding: utf-8 -*- from django.shortcuts import render from django.views.decorators import csrf # Received POST request data DEF search_post (Request): ctx ={} if request.POST: ctx['rlt'] = request.POST['q'] return render(request, "post.html", ctx)
urls.py rule modifying the following form: / the HelloWorld / the HelloWorld / urls.py file code: from django.conf.urls Import URL from . Import View, the testdb, Search, search2 urlpatterns = [ url(r'^hello$', view.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 HTTP: //127.0.0.1:8000/search-post display
After completion of the above example directory structure: 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 | |-- view.py | |-- view.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 Each view function the first argument is an HttpRequest object, as in this hello () function: from django.http import HttpResponse def hello(request): return HttpResponse("Hello world") HttpRequest object contains some information about the current request URL:
Request object also has some useful methods:
QueryDict objects
In HttpRequest object, GET and POST are examples django.http.QueryDict class attributes.
Similar dictionary QueryDict custom class for handling multiple values corresponding to the case where a single bond.
QueryDict implement all the standard dictionary methods. Also includes some unique ways:
In addition, there are some methods QueryDict the following table: