mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-06 05:20:12 +03:00
Added JinjaTemplateRenderer
This commit is contained in:
parent
21cb808569
commit
1956948c15
|
@ -200,6 +200,42 @@ class TemplateHTMLRenderer(BaseRenderer):
|
|||
response.status_text.title()))
|
||||
|
||||
|
||||
class JinjaTemplateRenderer(TemplateHTMLRenderer):
|
||||
"""
|
||||
Renders data to HTML for use with the Jinja2 template engine.
|
||||
|
||||
The template name is determined by (in order of preference):
|
||||
|
||||
1. An explicit .template_name set on the response.
|
||||
2. An explicit .template_name set on this class.
|
||||
3. The return result of calling view.get_template_names().
|
||||
"""
|
||||
|
||||
def render(self, data, accepted_media_type=None, renderer_context=None):
|
||||
|
||||
renderer_context = renderer_context or {}
|
||||
view = renderer_context['view']
|
||||
request = renderer_context['request']
|
||||
response = renderer_context['response']
|
||||
|
||||
# add the current user to the context
|
||||
# otherwise it won't be available in templates
|
||||
renderer_context['user'] = request.user
|
||||
|
||||
# get template (jinja2)
|
||||
if response.exception:
|
||||
template = self.get_exception_template(response)
|
||||
else:
|
||||
template_names = self.get_template_names(response, view)
|
||||
template = self.resolve_template(template_names)
|
||||
|
||||
# return the dict containing the context
|
||||
# for jinja2 to process.
|
||||
# Would throw a value error if a
|
||||
# RequestContext was returned
|
||||
return template.render(renderer_context)
|
||||
|
||||
|
||||
# Note, subclass TemplateHTMLRenderer simply for the exception behavior
|
||||
class StaticHTMLRenderer(TemplateHTMLRenderer):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user