Add missing imports and models to the examples in the view layer documentation

This commit is contained in:
Silvan Spross 2013-05-18 14:00:52 +02:00 committed by Marc Egli
parent e4591debd1
commit cd72c55d86
8 changed files with 57 additions and 2 deletions

View File

@ -215,6 +215,7 @@ re-rendered, you can re-evaluate the rendered content, and assign
the content of the response manually::
# Set up a rendered TemplateResponse
>>> from django.template.response import TemplateResponse
>>> t = TemplateResponse(request, 'original.html', {})
>>> t.render()
>>> print(t.content)
@ -256,6 +257,8 @@ To define a post-render callback, just define a function that takes
a single argument -- response -- and register that function with
the template response::
from django.template.response import TemplateResponse
def my_render_callback(response):
# Do content-sensitive processing
do_post_processing()

View File

@ -248,7 +248,7 @@ specify the objects that the view will operate upon -- you can also
specify the list of objects using the ``queryset`` argument::
from django.views.generic import DetailView
from books.models import Publisher, Book
from books.models import Publisher
class PublisherDetail(DetailView):
@ -326,6 +326,7 @@ various useful things are stored on ``self``; as well as the request
Here, we have a URLconf with a single captured group::
# urls.py
from django.conf.urls import patterns
from books.views import PublisherBookList
urlpatterns = patterns('',
@ -375,6 +376,7 @@ Imagine we had a ``last_accessed`` field on our ``Author`` object that we were
using to keep track of the last time anybody looked at that author::
# models.py
from django.db import models
class Author(models.Model):
salutation = models.CharField(max_length=10)
@ -390,6 +392,7 @@ updated.
First, we'd need to add an author detail bit in the URLconf to point to a
custom view::
from django.conf.urls import patterns, url
from books.views import AuthorDetailView
urlpatterns = patterns('',
@ -401,7 +404,6 @@ Then we'd write our new view -- ``get_object`` is the method that retrieves the
object -- so we simply override it and wrap the call::
from django.views.generic import DetailView
from django.shortcuts import get_object_or_404
from django.utils import timezone
from books.models import Author

View File

@ -222,6 +222,7 @@ works for AJAX requests as well as 'normal' form POSTs::
from django.http import HttpResponse
from django.views.generic.edit import CreateView
from myapp.models import Author
class AjaxableResponseMixin(object):
"""

View File

@ -258,6 +258,7 @@ mixin.
We can hook this into our URLs easily enough::
# urls.py
from django.conf.urls import patterns, url
from books.views import RecordInterest
urlpatterns = patterns('',
@ -440,6 +441,7 @@ Our new ``AuthorDetail`` looks like this::
from django.core.urlresolvers import reverse
from django.views.generic import DetailView
from django.views.generic.edit import FormMixin
from books.models import Author
class AuthorInterestForm(forms.Form):
message = forms.CharField()
@ -546,6 +548,8 @@ template as ``AuthorDisplay`` is using on ``GET``.
.. code-block:: python
from django.core.urlresolvers import reverse
from django.http import HttpResponseForbidden
from django.views.generic import FormView
from django.views.generic.detail import SingleObjectMixin
@ -657,6 +661,8 @@ own version of :class:`~django.views.generic.detail.DetailView` by mixing
:class:`~django.views.generic.detail.DetailView` before template
rendering behavior has been mixed in)::
from django.views.generic.detail import BaseDetailView
class JSONDetailView(JSONResponseMixin, BaseDetailView):
pass
@ -675,6 +681,8 @@ and override the implementation of
to defer to the appropriate subclass depending on the type of response that the
user requested::
from django.views.generic.detail import SingleObjectTemplateResponseMixin
class HybridDetailView(JSONResponseMixin, SingleObjectTemplateResponseMixin, BaseDetailView):
def render_to_response(self, context):
# Look for a 'format=json' GET argument

View File

@ -27,6 +27,8 @@ to deal with that file.
Consider the following model, using an :class:`~django.db.models.ImageField` to
store a photo::
from django.db import models
class Car(models.Model):
name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=5, decimal_places=2)

View File

@ -15,6 +15,7 @@ Basic file uploads
Consider a simple form containing a :class:`~django.forms.FileField`::
# In forms.py...
from django import forms
class UploadFileForm(forms.Form):
@ -39,6 +40,7 @@ something like::
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from .forms import UploadFileForm
# Imaginary function to handle an uploaded file.
from somewhere import handle_uploaded_file

View File

@ -123,6 +123,8 @@ is ``(?P<name>pattern)``, where ``name`` is the name of the group and
Here's the above example URLconf, rewritten to use named groups::
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^articles/2003/$', 'news.views.special_case_2003'),
url(r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
@ -192,6 +194,8 @@ A convenient trick is to specify default parameters for your views' arguments.
Here's an example URLconf and view::
# URLconf
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^blog/$', 'blog.views.page'),
url(r'^blog/page(?P<num>\d+)/$', 'blog.views.page'),
@ -370,11 +374,15 @@ An included URLconf receives any captured parameters from parent URLconfs, so
the following example is valid::
# In settings/urls/main.py
from django.conf.urls import include, patterns, url
urlpatterns = patterns('',
url(r'^(?P<username>\w+)/blog/', include('foo.urls.blog')),
)
# In foo/urls/blog.py
from django.conf.urls import patterns, url
urlpatterns = patterns('foo.views',
url(r'^$', 'blog.index'),
url(r'^archive/$', 'blog.archive'),
@ -397,6 +405,8 @@ function.
For example::
from django.conf.urls import patterns, url
urlpatterns = patterns('blog.views',
url(r'^blog/(?P<year>\d{4})/$', 'year_archive', {'foo': 'bar'}),
)
@ -427,11 +437,15 @@ For example, these two URLconf sets are functionally identical:
Set one::
# main.py
from django.conf.urls import include, patterns, url
urlpatterns = patterns('',
url(r'^blog/', include('inner'), {'blogid': 3}),
)
# inner.py
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^archive/$', 'mysite.views.archive'),
url(r'^about/$', 'mysite.views.about'),
@ -440,11 +454,15 @@ Set one::
Set two::
# main.py
from django.conf.urls import include, patterns, url
urlpatterns = patterns('',
url(r'^blog/', include('inner')),
)
# inner.py
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^archive/$', 'mysite.views.archive', {'blogid': 3}),
url(r'^about/$', 'mysite.views.about', {'blogid': 3}),
@ -464,6 +482,8 @@ supported -- you can pass any callable object as the view.
For example, given this URLconf in "string" notation::
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^archive/$', 'mysite.views.archive'),
url(r'^about/$', 'mysite.views.about'),
@ -473,6 +493,7 @@ For example, given this URLconf in "string" notation::
You can accomplish the same thing by passing objects rather than strings. Just
be sure to import the objects::
from django.conf.urls import patterns, url
from mysite.views import archive, about, contact
urlpatterns = patterns('',
@ -485,6 +506,7 @@ The following example is functionally identical. It's just a bit more compact
because it imports the module that contains the views, rather than importing
each view individually::
from django.conf.urls import patterns, url
from mysite import views
urlpatterns = patterns('',
@ -501,6 +523,7 @@ the view prefix (as explained in "The view prefix" above) will have no effect.
Note that :doc:`class based views</topics/class-based-views/index>` must be
imported::
from django.conf.urls import patterns, url
from mysite.views import ClassBasedView
urlpatterns = patterns('',
@ -612,6 +635,9 @@ It's fairly common to use the same view function in multiple URL patterns in
your URLconf. For example, these two URL patterns both point to the ``archive``
view::
from django.conf.urls import patterns, url
from mysite.views import archive
urlpatterns = patterns('',
url(r'^archive/(\d{4})/$', archive),
url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}),
@ -630,6 +656,9 @@ matching.
Here's the above example, rewritten to use named URL patterns::
from django.conf.urls import patterns, url
from mysite.views import archive
urlpatterns = patterns('',
url(r'^archive/(\d{4})/$', archive, name="full-archive"),
url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}, name="arch-summary"),
@ -803,6 +832,8 @@ However, you can also ``include()`` a 3-tuple containing::
For example::
from django.conf.urls import include, patterns, url
help_patterns = patterns('',
url(r'^basic/$', 'apps.help.views.views.basic'),
url(r'^advanced/$', 'apps.help.views.views.advanced'),

View File

@ -70,6 +70,8 @@ documentation. Just return an instance of one of those subclasses instead of
a normal :class:`~django.http.HttpResponse` in order to signify an error. For
example::
from django.http import HttpResponse, HttpResponseNotFound
def my_view(request):
# ...
if foo:
@ -83,6 +85,8 @@ the :class:`~django.http.HttpResponse` documentation, you can also pass the
HTTP status code into the constructor for :class:`~django.http.HttpResponse`
to create a return class for any status code you like. For example::
from django.http import HttpResponse
def my_view(request):
# ...
@ -110,6 +114,8 @@ standard error page for your application, along with an HTTP error code 404.
Example usage::
from django.http import Http404
from django.shortcuts import render_to_response
from polls.models import Poll
def detail(request, poll_id):
try: