Appendix C. Generic View Reference

Chapter 10, Generic Views, introduced generic views but left out some of the gory details. This appendix describes each generic view along with a summary of options each view can take. Be sure to read Chapter 10, Generic Views, before trying to understand the reference material that follows. You might want to refer back to the Book, Publisher, and Author objects defined in that chapter; the examples that follow use these models. If you want to dig deeper into more advanced generic view topics (like using mixins with the class-based views), see the Django Project website at https://docs.djangoproject.com/en/1.8/topics/class-based-views/.

Common arguments to generic views

Most of these views take a large number of arguments that can change the generic view's behavior. Many of these arguments work the same across multiple views. Table C.1 describes each of these common arguments; anytime you see one of these arguments in a generic view's argument list, it will work as described in the table.

Argument

Description

allow_empty

A Boolean specifying whether to display the page if no objects are available. If this is False and no objects are available, the view will raise a 404 error instead of displaying an empty page. By default, this is True.

context_processors

A list of additional template-context processors (besides the defaults) to apply to the view's template. See Chapter 9, Advanced Models, for information on template context processors.

extra_context

A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the generic view will call it just before rendering the template.

mimetype

The MIME type to use for the resulting document. It defaults to the value of the DEFAULT_MIME_TYPE setting, which is text/html if you haven't changed it.

queryset

A QuerySet (that is, something like Author.objects.all()) to read objects from. See Appendix B for more information about QuerySet objects. Most generic views require this argument.

template_loader

The template loader to use when loading the template. By default, it's django.template.loader. See Chapter 9, Advanced Models, for information on template loaders.

template_name

The full name of a template to use in rendering the page. This lets you override the default template name derived from the QuerySet.

template_object_name

The name of the template variable to use in the template context. By default, this is 'object'. Views that list more than one object (that is, object_list views and various objects-for-date views) will append '_list' to the value of this parameter.

Table C.1: Common Generic View Arguments

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset