[Notes] Django Django Quick Start Section 4: Forms and generic views
Notes on official documents and technology blog , extract key information, and record notes, see the official documentation routines.
Complete official document: Django Documentation
Recommended blog: Django2.2 Tutorial
Django Getting Started official document: Part 4: Forms and the Generic views
table of Contents
2. General view of: reducing code duplication
1. Form form
For the template file, points :
- form tag in, Action is the destination url you want to send, method represents the submission of the data, normally post and get. When you need to change the form to create a server-side data, use method = "post"
forloop.counter
Indicatingfor
label has cycled many times . forloop.counter is a variable DJango template system specially provided to indicate the number of your current cycle, the cycle is generally used to add items ordered logarithmic scale.- When you create a POST form (which has the effect of modifying data), you need to be careful cross-site request forgery, referred to CSRF. Django has a defense to its system very easy to use. In short, all the internal URL for the POST form should use template tags
{% csrf_token %}
. (Label name can not be changed, fixed format, in any position, as long as the form is in the form. This method is convenient Haoshi submission form on the form of the way, but if it is submitting data using ajax way, then do not use this method has.)
Submitted form data is processed in the view, view points :
-
request.POST
Is a dictionary-like object that allows access to the data key name submitted. For examplerequest.POST['choice']
in a return string selection of Choice's ID. Note:request.POST
The value is always a string. -
Django also in the same manner provided
request.GET
for accessing GET data. -
If you
request.POST['choice']
do not provide the datachoice
, POST will lead to aKeyError
. PS: Usually we give a default value, to prevent such an exception, for examplerequest.POST[’choice’,None]
, a None solve all the problems. -
HttpResponseRedirect
Receiving only one parameter : the user will be redirected to the URL of . Note: After successful processing of POST data, should maintain a good habit, always returns a HttpResponseRedirect. Such as:return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
-
reverse()
function. This function avoids us hard-coded URL in the view function. It requires us to give the name of the view you want to jump and URL pattern corresponding to the view of the need to provide a view of the parameters ( data that is passed ) . (Eg: after redirection code above into thepolls:results
corresponding view , andquestion.id
pass it to the vernacular terms, is to additionally throw a live view corresponding to the route to get there..)
2. General view of: reducing code duplication
Web development is a common situation:
- Get data from the database according to parameters in the URL,
- Load the template file,
- Just use the data to render a template, return to this template.
Because of this situation is particularly common, Django provides a shortcut, called the " generic views " system.
The original code instead of using a common view mode , needs to be done to complete the conversion of the following steps:
- Converting the URLconf .
- Delete some of the old view is no longer needed.
- The new view is introduced based on a common view of Django .
(Note: Under development should begin to determine whether the use of generic views, rather than halfway reconstructed)
Need to import generic views:
from django.views import generic
General view :( as they are inherited parent class)
ListView
: Stands for "displays a list of objects." ListView generic view uses a template called default attribute is used to specify the template name.<app name>/<model name>_list.html。template_name
DetailView
: Stands for "details page of a particular type of object." By default, the DetailView
general view is referred to using a <app name>/<model name>_detail.html
template (eg: polls / detail.html ). template_name
Attribute is used to specify the name of the template used in place of the default template name is automatically generated.
class DetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'
- Each generic view needs to know which model it will act on. This is done by
model
providing property. DetailView
Capture named expect from a URL"pk"
primary key value. (So for example the general viewquestion_id
changepk
).
Class view is a more advanced Django usage, there is a first impression. More detailed information on generic views, see the document generic views .