Simple mixinsLink to this heading

ContextMixinLink to this heading

class django.views.generic.base.ContextMixinLink to this definition

Attributes

extra_contextLink to this definition

A dictionary to include in the context. This is a convenient way of specifying some context in as_view(). Example usage:

Code
from django.views.generic import TemplateView
TemplateView.as_view(extra_context={'title': 'Custom Title'})

Methods

get_context_data(**kwargs)Link to this definition

Returns a dictionary representing the template context. The keyword arguments provided will make up the returned context. Example usage:

Code
def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['number'] = random.randrange(1, 100)
    return context

The template context of all class-based generic views include a view variable that points to the View instance.

TemplateResponseMixinLink to this heading

class django.views.generic.base.TemplateResponseMixinLink to this definition

Provides a mechanism to construct a TemplateResponse, given suitable context. The template to use is configurable and can be further customized by subclasses.

Attributes

template_nameLink to this definition

The full name of a template to use as defined by a string. Not defining a template_name will raise a django.core.exceptions.ImproperlyConfigured exception.

template_engineLink to this definition

The NAME of a template engine to use for loading the template. template_engine is passed as the using keyword argument to response_class. Default is None, which tells Django to search for the template in all configured engines.

response_classLink to this definition

The response class to be returned by render_to_response method. Default is TemplateResponse. The template and context of TemplateResponse instances can be altered later (e.g. in template response middleware).

If you need custom template loading or custom context object instantiation, create a TemplateResponse subclass and assign it to response_class.

content_typeLink to this definition

The content type to use for the response. content_type is passed as a keyword argument to response_class. Default is None – meaning that Django uses 'text/html'.

Methods

render_to_response(context, **response_kwargs)Link to this definition

Returns a self.response_class instance.

If any keyword arguments are provided, they will be passed to the constructor of the response class.

Calls get_template_names() to obtain the list of template names that will be searched looking for an existent template.

get_template_names()Link to this definition

Returns a list of template names to search for when rendering the template. The first template that is found will be used.

The default implementation will return a list containing template_name (if it is specified).