"Use generic views: Less code is better" - FAQ

Issue:

I am a new newbie to Django. Great tutorial BTW. I followed it to the T and 99.9% worked. Except the last step in the Advanced tutorial, the step 3 in 'Using your own package'. I could see http://127.0.0.1:8000/admin/ and create polls without any problem. But http://127.0.0.1:8000/polls/ would give me the following error.
I see there are two problems.
1.  /Users/nnn/django_exercises/mysite/templates/polls/index.html is the wrong location, it should be $$/django_polls/polls/templates/polls/index.html.
2. What is polls/poll_list.html?   Nowhere in the tutorial ever created this file, no?

example code:

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic

from polls.models import Choice, Poll

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_poll_list'

    def get_queryset(self):
        """Return the last five published polls."""
        return Poll.objects.order_by('-pub_date')[:5]


class DetailView(generic.DetailView):
    model = Poll
    template_name = 'polls/detail.html'


class ResultsView(generic.DetailView):
    model = Poll
    template_name = 'polls/results.html'

def vote(request, poll_id):
We’re using two generic views here:  ListView  and  DetailView . Respectively, those two views abstract the concepts of “display a list of objects” and “display a detail page for a particular type of object.”

Each generic view needs to know what model it will be acting upon. This is provided using the model attribute.
The DetailView generic view expects the primary key value captured from the URL to be called "pk", so we’ve changed poll_id to pk for the generic views.

By default, the DetailView generic view uses a template called <app name>/<model name>_detail.html. In our case, it’ll use the template "polls/poll_detail.html". The template_name attribute is used to tell Django to use a specific template name instead of the autogenerated default template name. We also specify the template_name for the results list view – this ensures that the results view and the detail view have a different appearance when rendered, even though they’re both aDetailView behind the scenes.

Similarly, the ListView generic view uses a default template called <app name>/<model name>_list.html; we usetemplate_name to tell ListView to use our existing "polls/index.html" template.

In previous parts of the tutorial, the templates have been provided with a context that contains the poll andlatest_poll_list context variables. For DetailView the poll variable is provided automatically – since we’re using a Django model (Poll), Django is able to determine an appropriate name for the context variable. However, for ListView, the automatically generated context variable is poll_list. To override this we provide the context_object_name attribute, specifying that we want to use latest_poll_list instead. As an alternative approach, you could change your templates to match the new default context variables – but it’s a lot easier to just tell Django to use the variable you want.

Run the server, and use your new polling app based on generic views.

For full details on generic views, see the generic views documentation.

你可能感兴趣的:(django,views,模板路径问题)