The general-purpose view allows you to create pages even shorter than shortcuts. http://docs.djangoproject.jp/en/latest/ref/generic-views.html
I've written a lot in views.py so far, but with a generic view you can shave it off.
Edit urls.py on the application side as follows.
polls/urls.py ######
from django.conf.urls import patterns, include, url
from django.views.generic import DetailView, ListView
from polls.models import Poll
urlpatterns = patterns('', #Don't forget to return it to the empty state
url(r'^$',
ListView.as_view(
queryset=Poll.objects.order_by('-pub_date')[:5],
context_object_name='latest_poll_list',
template_name='polls/index.html'
)
),
url(r'^(?P<pk>\d+)/$',
DetailView.as_view(
model=Poll,
template_name='polls/detail.html'
)
),
url(r'^(?P<pk>\d+)/results/$',
DetailView.as_view(
model=Poll,
template_name='polls/results.html'
),
name='poll_results'
),
url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
)
Here, ListView and DetailView are shown.
First of all, in common, until now, on the views.py side
# def index..
temp = loader.get_template('polls/index.html')
And
return render_to_response('polls/detail.html',{ #..
The template was specified in the function like this,
# url(..
# SomeView.as_view(..
template_name = 'polls/index.html'
Substitute with one sentence. Each template is specified directly in URLconf. If you do not specify this, ‘appName / modelName_detail.html’ seems to be the default name, but creating a template with this file name is not good enough. Especially in this example, the template name is fogged by detail and results, so it's definitely not good.
Next, let's look at each type of general-purpose view.
--queryset: Enter the data you want to display in the list (array).
--context_object_name: Specify the name of the queryset (if not specified, the name modelName_list is the default). In the template created so far, the received name was ‘latest_poll_list’, so change it to this (even if you edit the name on the template side, the result is the same).
DetailView。 #####
First, notice that the first argument of the url has changed slightly.
# before
r'^(?P<poll_id>\d+)/$'
# after
r'^(?P<pk>\d+)/$'
The data with ‘\ d +’ (one or more digits) as pk (PrimaryKey) is reflected in the template. Pk here is a cliché. There seems to be a slug, but I'm not sure at the moment.
--model: As it is, the model class is specified.
--name: You have set a name for the view. It seems that a URL that calls this view can be automatically generated (named pattern?).
Editing of URLconf is now complete. And if you've done so far
It's okay to erase it. However, let's leave only vote. And edit only one line as follows.
views.py ######
#def vote..
return HttpResponseRedirect( reverse('poll_results', args=(pobject.id,)) )
The first argument of reverse has been changed from'polls.views.results' to'poll_results'. This is the name of the results page you set earlier, and reverse seems to automatically generate a URL according to the view from here.
If you check the operation, you can see that it works exactly the same as Last time. Obviously this is easier.
For the time being, I'd like to review it with the book I was referring to in the past post DJango Memo: Basics of Blog Creation and try again at the same time.
Recommended Posts