class SingleObjectTemplateResponseMixin

from django.views.generic.detail import SingleObjectTemplateResponseMixin

Ancestors (MRO)

  1. SingleObjectTemplateResponseMixin
  2. TemplateResponseMixin

Attributes

  Defined in
response_class = <class 'django.template.response.TemplateResponse'> 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. Must return
a list. May not be called if get_template is overridden.
107
108
109
110
111
112
113
114
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
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 get_template is overridden.
    """
    try:
        names = super(SingleObjectTemplateResponseMixin, self).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 hasattr(self.object, '_meta'):
        names.append("%s/%s%s.html" % (
            self.object._meta.app_label,
            self.object._meta.object_name.lower(),
            self.template_name_suffix
        ))
    elif hasattr(self, 'model') and hasattr(self.model, '_meta'):
        names.append("%s/%s%s.html" % (
            self.model._meta.app_label,
            self.model._meta.object_name.lower(),
            self.template_name_suffix
        ))
    return names

TemplateResponseMixin

Returns 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.
 99
100
101
102
103
104
105
106
107
108
109
def get_template_names(self):
    """
    Returns 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

Returns a response with a template rendered with the given context.
88
89
90
91
92
93
94
95
96
97
def render_to_response(self, context, **response_kwargs):
    """
    Returns a response with a template rendered with the given context.
    """
    return self.response_class(
        request = self.request,
        template = self.get_template_names(),
        context = context,
        **response_kwargs
    )