Add missing imports to the examples in the 'First Steps'

This commit is contained in:
leandrafinger 2013-05-18 13:34:29 +02:00 committed by Marc Egli
parent cd72c55d86
commit ddd9ee16fa
5 changed files with 29 additions and 0 deletions

View File

@ -24,6 +24,8 @@ representing your models -- so far, it's been solving two years' worth of
database-schema problems. Here's a quick example, which might be saved in database-schema problems. Here's a quick example, which might be saved in
the file ``mysite/news/models.py``:: the file ``mysite/news/models.py``::
from django.db import models
class Reporter(models.Model): class Reporter(models.Model):
full_name = models.CharField(max_length=70) full_name = models.CharField(max_length=70)
@ -214,6 +216,8 @@ Generally, a view retrieves data according to the parameters, loads a template
and renders the template with the retrieved data. Here's an example view for and renders the template with the retrieved data. Here's an example view for
``year_archive`` from above:: ``year_archive`` from above::
from django.shortcuts import render_to_response
def year_archive(request, year): def year_archive(request, year):
a_list = Article.objects.filter(pub_date__year=year) a_list = Article.objects.filter(pub_date__year=year)
return render_to_response('news/year_archive.html', {'year': year, 'article_list': a_list}) return render_to_response('news/year_archive.html', {'year': year, 'article_list': a_list})

View File

@ -582,6 +582,8 @@ of this object. Let's fix that by editing the polls model (in the
``Choice``. On Python 3, simply replace ``__unicode__`` by ``__str__`` in the ``Choice``. On Python 3, simply replace ``__unicode__`` by ``__str__`` in the
following example:: following example::
from django.db import models
class Poll(models.Model): class Poll(models.Model):
# ... # ...
def __unicode__(self): # Python 3: def __str__(self): def __unicode__(self): # Python 3: def __str__(self):

View File

@ -158,6 +158,9 @@ you want when you register the object.
Let's see how this works by re-ordering the fields on the edit form. Replace Let's see how this works by re-ordering the fields on the edit form. Replace
the ``admin.site.register(Poll)`` line with:: the ``admin.site.register(Poll)`` line with::
from django.contrib import admin
from polls.models import Poll
class PollAdmin(admin.ModelAdmin): class PollAdmin(admin.ModelAdmin):
fields = ['pub_date', 'question'] fields = ['pub_date', 'question']
@ -179,6 +182,9 @@ of fields, choosing an intuitive order is an important usability detail.
And speaking of forms with dozens of fields, you might want to split the form And speaking of forms with dozens of fields, you might want to split the form
up into fieldsets:: up into fieldsets::
from django.contrib import admin
from polls.models import Poll
class PollAdmin(admin.ModelAdmin): class PollAdmin(admin.ModelAdmin):
fieldsets = [ fieldsets = [
(None, {'fields': ['question']}), (None, {'fields': ['question']}),
@ -198,6 +204,9 @@ You can assign arbitrary HTML classes to each fieldset. Django provides a
This is useful when you have a long form that contains a number of fields that This is useful when you have a long form that contains a number of fields that
aren't commonly used:: aren't commonly used::
from django.contrib import admin
from polls.models import Poll
class PollAdmin(admin.ModelAdmin): class PollAdmin(admin.ModelAdmin):
fieldsets = [ fieldsets = [
(None, {'fields': ['question']}), (None, {'fields': ['question']}),
@ -218,6 +227,7 @@ Yet.
There are two ways to solve this problem. The first is to register ``Choice`` There are two ways to solve this problem. The first is to register ``Choice``
with the admin just as we did with ``Poll``. That's easy:: with the admin just as we did with ``Poll``. That's easy::
from django.contrib import admin
from polls.models import Choice from polls.models import Choice
admin.site.register(Choice) admin.site.register(Choice)
@ -342,6 +352,12 @@ representation of the output.
You can improve that by giving that method (in :file:`polls/models.py`) a few You can improve that by giving that method (in :file:`polls/models.py`) a few
attributes, as follows:: attributes, as follows::
import datetime
from django.utils import timezone
from django.db import models
from polls.models import Poll
class Poll(models.Model): class Poll(models.Model):
# ... # ...
def was_published_recently(self): def was_published_recently(self):

View File

@ -393,6 +393,9 @@ Now, let's tackle the poll detail view -- the page that displays the question
for a given poll. Here's the view:: for a given poll. Here's the view::
from django.http import Http404 from django.http import Http404
from django.shortcuts import render
from polls.models import Poll
# ... # ...
def detail(request, poll_id): def detail(request, poll_id):
try: try:
@ -420,6 +423,8 @@ and raise :exc:`~django.http.Http404` if the object doesn't exist. Django
provides a shortcut. Here's the ``detail()`` view, rewritten:: provides a shortcut. Here's the ``detail()`` view, rewritten::
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from polls.models import Poll
# ... # ...
def detail(request, poll_id): def detail(request, poll_id):
poll = get_object_or_404(Poll, pk=poll_id) poll = get_object_or_404(Poll, pk=poll_id)

View File

@ -136,6 +136,8 @@ object. For more on :class:`~django.http.HttpRequest` objects, see the
After somebody votes in a poll, the ``vote()`` view redirects to the results After somebody votes in a poll, the ``vote()`` view redirects to the results
page for the poll. Let's write that view:: page for the poll. Let's write that view::
from django.shortcuts import get_object_or_404, render
def results(request, poll_id): def results(request, poll_id):
poll = get_object_or_404(Poll, pk=poll_id) poll = get_object_or_404(Poll, pk=poll_id)
return render(request, 'polls/results.html', {'poll': poll}) return render(request, 'polls/results.html', {'poll': poll})