Simple mixins¶
ContextMixin¶
-
class
django.views.generic.base.ContextMixin¶ Methods
-
get_context_data(**kwargs)¶ Returns a dictionary representing the template context. The keyword arguments provided will make up the returned context. Example usage:
def get_context_data(self, **kwargs): context = super(RandomNumberView, self).get_context_data(**kwargs) context['number'] = random.randrange(1, 100) return context
The template context of all class-based generic views include a
viewvariable that points to theViewinstance.Use
alters_datawhere appropriateNote that having the view instance in the template context may expose potentially hazardous methods to template authors. To prevent methods like this from being called in the template, set
alters_data=Trueon those methods. For more information, read the documentation on rendering a template context.
-
TemplateResponseMixin¶
-
class
django.views.generic.base.TemplateResponseMixin¶ 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_name¶ The full name of a template to use as defined by a string. Not defining a
template_namewill raise adjango.core.exceptions.ImproperlyConfiguredexception.
-
template_engine¶ - New in Django 1.8.
The
NAMEof a template engine to use for loading the template.template_engineis passed as theusingkeyword argument toresponse_class. Default isNone, which tells Django to search for the template in all configured engines.
-
response_class¶ The response class to be returned by
render_to_responsemethod. Default isTemplateResponse. The template and context ofTemplateResponseinstances can be altered later (e.g. in template response middleware).Changed in Django 1.8:In older versions of Django,
TemplateResponseusedRequestContextin such a way that values from template context processors would override template variables defined in your views. For example, if you subclassedDetailViewand setcontext_object_nametouser, thedjango.contrib.auth.context_processors.authcontext processor would overwrite your variable with the current user. Now, for consistency with therender()shortcut, values in the context provided by the class override values from context processors.If you need custom template loading or custom context object instantiation, create a
TemplateResponsesubclass and assign it toresponse_class.
-
content_type¶ The content type to use for the response.
content_typeis passed as a keyword argument toresponse_class. Default isNone– meaning that Django usesDEFAULT_CONTENT_TYPE.
Methods
-
render_to_response(context, **response_kwargs)¶ Returns a
self.response_classinstance.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()¶ Returns a list of template names to search for when rendering the template.
If
template_nameis specified, the default implementation will return a list containingtemplate_name(if it is specified).
-