diff options
Diffstat (limited to 'docs/topics/class-based-views/index.txt')
| -rw-r--r-- | docs/topics/class-based-views/index.txt | 50 |
1 files changed, 24 insertions, 26 deletions
diff --git a/docs/topics/class-based-views/index.txt b/docs/topics/class-based-views/index.txt index 17e8b66f8f..364746a093 100644 --- a/docs/topics/class-based-views/index.txt +++ b/docs/topics/class-based-views/index.txt @@ -6,10 +6,10 @@ A view is a callable which takes a request and returns a response. This can be more than just a function, and Django provides an example of some classes which can be used as views. These allow you to structure your views and reuse code by harnessing inheritance and -mixins. There are also some generic views for simple tasks which we'll -get to later, but you may want to design your own structure of -reusable views which suits your use case. For full details, see the -:doc:`class-based views reference documentation</ref/class-based-views/index>`. +mixins. There are also some generic views for tasks which we'll get to later, +but you may want to design your own structure of reusable views which suits +your use case. For full details, see the :doc:`class-based views reference +documentation</ref/class-based-views/index>`. .. toctree:: :maxdepth: 1 @@ -25,18 +25,18 @@ Basic examples Django provides base view classes which will suit a wide range of applications. All views inherit from the :class:`~django.views.generic.base.View` class, which handles linking the view in to the URLs, HTTP method dispatching and other -simple features. :class:`~django.views.generic.base.RedirectView` is for a -simple HTTP redirect, and :class:`~django.views.generic.base.TemplateView` -extends the base class to make it also render a template. +common features. :class:`~django.views.generic.base.RedirectView` provides a +HTTP redirect, and :class:`~django.views.generic.base.TemplateView` extends the +base class to make it also render a template. -Simple usage in your URLconf -============================ +Usage in your URLconf +===================== -The simplest way to use generic views is to create them directly in your -URLconf. If you're only changing a few simple attributes on a class-based view, -you can simply pass them into the -:meth:`~django.views.generic.base.View.as_view` method call itself:: +The most direct way to use generic views is to create them directly in your +URLconf. If you're only changing a few attributes on a class-based view, you +can pass them into the :meth:`~django.views.generic.base.View.as_view` method +call itself:: from django.urls import path from django.views.generic import TemplateView @@ -59,8 +59,8 @@ existing view and override attributes (such as the ``template_name``) or methods (such as ``get_context_data``) in your subclass to provide new values or methods. Consider, for example, a view that just displays one template, ``about.html``. Django has a generic view to do this - -:class:`~django.views.generic.base.TemplateView` - so we can just subclass it, -and override the template name:: +:class:`~django.views.generic.base.TemplateView` - so we can subclass it, and +override the template name:: # some_app/views.py from django.views.generic import TemplateView @@ -68,11 +68,10 @@ and override the template name:: class AboutView(TemplateView): template_name = "about.html" -Then we just need to add this new view into our URLconf. -:class:`~django.views.generic.base.TemplateView` is a class, not a function, -so we point the URL to the :meth:`~django.views.generic.base.View.as_view` -class method instead, which provides a function-like entry to class-based -views:: +Then we need to add this new view into our URLconf. +:class:`~django.views.generic.base.TemplateView` is a class, not a function, so +we point the URL to the :meth:`~django.views.generic.base.View.as_view` class +method instead, which provides a function-like entry to class-based views:: # urls.py from django.urls import path @@ -123,9 +122,8 @@ And the view:: response['Last-Modified'] = last_book.publication_date.strftime('%a, %d %b %Y %H:%M:%S GMT') return response -If the view is accessed from a ``GET`` request, a plain-and-simple object -list is returned in the response (using ``book_list.html`` template). But if -the client issues a ``HEAD`` request, the response has an empty body and -the ``Last-Modified`` header indicates when the most recent book was published. -Based on this information, the client may or may not download the full object -list. +If the view is accessed from a ``GET`` request, an object list is returned in +the response (using the ``book_list.html`` template). But if the client issues +a ``HEAD`` request, the response has an empty body and the ``Last-Modified`` +header indicates when the most recent book was published. Based on this +information, the client may or may not download the full object list. |
