Commit cd72c55d authored by Silvan Spross's avatar Silvan Spross Committed by Marc Egli
Browse files

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

parent e4591deb
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -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()
+4 −2
Original line number Diff line number Diff line
@@ -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

+1 −0
Original line number Diff line number Diff line
@@ -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):
        """
+8 −0
Original line number Diff line number Diff line
@@ -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
+2 −0
Original line number Diff line number Diff line
@@ -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)
Loading