class SingleObjectTemplateResponseMixin

from django.views.generic.detail import SingleObjectTemplateResponseMixin
A mixin that can be used to render a template.

Ancestors (MRO)

  1. SingleObjectTemplateResponseMixin
  2. TemplateResponseMixin

Attributes

  Defined in
content_type = None TemplateResponseMixin
response_class = <class 'django.template.response.TemplateResponse'> TemplateResponseMixin
template_engine = None TemplateResponseMixin
template_name = None TemplateResponseMixin
template_name_field = None SingleObjectTemplateResponseMixin
template_name_suffix = '_detail' SingleObjectTemplateResponseMixin
Expand Collapse

Methods

def get_template_names(self):

SingleObjectTemplateResponseMixin

Return a list of template names to be used for the request. May not be
called if render_to_response() is overridden. Return the following list:

* the value of ``template_name`` on the view (if provided)
* the contents of the ``template_name_field`` field on the
  object instance that the view is operating upon (if available)
* ``<app_label>/<model_name><template_name_suffix>.html``
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
def get_template_names(self):
    """
    Return a list of template names to be used for the request. May not be
    called if render_to_response() is overridden. Return the following list:
    * the value of ``template_name`` on the view (if provided)
    * the contents of the ``template_name_field`` field on the
      object instance that the view is operating upon (if available)
    * ``<app_label>/<model_name><template_name_suffix>.html``
    """
    try:
        names = super().get_template_names()
    except ImproperlyConfigured:
        # If template_name isn't specified, it's not a problem --
        # we just start with an empty list.
        names = []
        # If self.template_name_field is set, grab the value of the field
        # of that name from the object; this is the most specific template
        # name, if given.
        if self.object and self.template_name_field:
            name = getattr(self.object, self.template_name_field, None)
            if name:
                names.insert(0, name)
        # The least-specific option is the default <app>/<model>_detail.html;
        # only use this if the object in question is a model.
        if isinstance(self.object, models.Model):
            object_meta = self.object._meta
            names.append("%s/%s%s.html" % (
                object_meta.app_label,
                object_meta.model_name,
                self.template_name_suffix
            ))
        elif getattr(self, 'model', None) is not None and issubclass(self.model, models.Model):
            names.append("%s/%s%s.html" % (
                self.model._meta.app_label,
                self.model._meta.model_name,
                self.template_name_suffix
            ))
        # If we still haven't managed to find any template names, we should
        # re-raise the ImproperlyConfigured to alert the user.
        if not names:
            raise
    return names

TemplateResponseMixin

Return a list of template names to be used for the request. Must return
a list. May not be called if render_to_response() is overridden.
131
132
133
134
135
136
137
138
139
140
141
def get_template_names(self):
    """
    Return a list of template names to be used for the request. Must return
    a list. May not be called if render_to_response() is overridden.
    """
    if self.template_name is None:
        raise ImproperlyConfigured(
            "TemplateResponseMixin requires either a definition of "
            "'template_name' or an implementation of 'get_template_names()'")
    else:
        return [self.template_name]

def render_to_response(self, context, **response_kwargs): TemplateResponseMixin

Return a response, using the `response_class` for this view, with a
template rendered with the given context.

Pass response_kwargs to the constructor of the response class.
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def render_to_response(self, context, **response_kwargs):
    """
    Return a response, using the `response_class` for this view, with a
    template rendered with the given context.
    Pass response_kwargs to the constructor of the response class.
    """
    response_kwargs.setdefault('content_type', self.content_type)
    return self.response_class(
        request=self.request,
        template=self.get_template_names(),
        context=context,
        using=self.template_engine,
        **response_kwargs
    )