This article mainly introduces the form processing example in the Python Django framework. form processing is a basic operation in Django. if you need it, you can refer to the following example about books, authors, and publishing houses:
from django.db import modelsclass Publisher(models.Model): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60) state_province = models.CharField(max_length=30) country = models.CharField(max_length=50) website = models.URLField()class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) email = models.EmailField()class Book(models.Model): title = models.CharField(max_length=100) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publication_date = models.DateField()
Now we create a simple view function so that users can search for books from the database by name.
Generally, form development is divided into two parts: the front-end HTML page user interface and the background view function to process the submitted data. The first part is very simple. now we create a view to display a search form:
from django.shortcuts import render_to_responsedef search_form(request): return render_to_response('search_form.html')
This view function can be stored anywhere in the search path of Python. To facilitate the discussion, we will put it in books/views. py.
The search_form.html template may look like this:
Search
The URLpattern in urls. py may be like this:
from mysite.books import viewsurlpatterns = patterns('', # ... (r'^search-form/$', views.search_form), # ...)
(Note: we directly import the views module, instead of using statements like from mysite. views import search_form, because the former looks more concise .)
Now, if you run the runserver command and access http: // 127.0.0.1: 8000/search-form/, you will see the search interface. Very simple.
However, when you submit data through this form, you will get a Django 404 error. The URL/search/to which this Form points has not been implemented yet. Let's add the second view function and set the URL:
# urls.pyurlpatterns = patterns('', # ... (r'^search-form/$', views.search_form), (r'^search/$', views.search), # ...)# views.pydef search(request): if 'q' in request.GET: message = 'You searched for: %r' % request.GET['q'] else: message = 'You submitted an empty form.' return HttpResponse(message)
Currently, only the words searched by the user are displayed to confirm that the search data is correctly submitted to Django, so that you will know how the search data is transmitted in this system. In short:
In HTML, we define a variable q. When a form is submitted, the q value is appended to URL/search/Through GET (method = "get.
Request. GET is used to obtain the q value in the/search/(search () view.
It should be noted that q is included in request. GET. As mentioned in the request. META section above, data submitted by users and even correct data must be filtered. If no detection is performed here, submitting an empty form will cause a KeyError exception:
# BAD!def bad_search(request): # The following line will raise KeyError if 'q' hasn't # been submitted! message = 'You searched for: %r' % request.GET['q'] return HttpResponse(message)
Query string parameters
Because the data using the GET method is transmitted by querying strings (for example,/search /? Q = django), so we can use requet. GET to obtain the data. We know that you can use request. GET in the view to obtain the query string in a traditional URL (for example, hours = 3 ).
The data obtained using the POST method is similar to the GET method, but request. POST is used instead of request. GET. So what is the difference between POST and GET? You can use GET when submitting a form to retrieve data. when submitting a form, you must change the server data status or send an email, or use POST instead of obtaining and displaying data. In this book search example, we use GET because this query does not change the server data status. (If you are interested in