mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 00:19:53 +03:00
Extend template context in TemplateHTMLRenderer
* Pass data as a whole. * Pass processing response. * Pass current view. * Request is always available in Django templates. Actually, the data can be accessed trough the response, the request and the response trough the view.
This commit is contained in:
parent
ad282da97c
commit
4c877d2170
|
@ -169,10 +169,16 @@ class TemplateHTMLRenderer(BaseRenderer):
|
||||||
return loader.select_template(template_names)
|
return loader.select_template(template_names)
|
||||||
|
|
||||||
def get_template_context(self, data, renderer_context):
|
def get_template_context(self, data, renderer_context):
|
||||||
|
result = {k: renderer_context[k] for k in ('request', 'response', 'view')}
|
||||||
|
result['data'] = data
|
||||||
|
try:
|
||||||
|
result.update(data) # for compatibility
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
response = renderer_context['response']
|
response = renderer_context['response']
|
||||||
if response.exception:
|
if response.exception:
|
||||||
data['status_code'] = response.status_code
|
result['status_code'] = response.status_code
|
||||||
return data
|
return result
|
||||||
|
|
||||||
def get_template_names(self, response, view):
|
def get_template_names(self, response, view):
|
||||||
if response.template_name:
|
if response.template_name:
|
||||||
|
|
|
@ -615,6 +615,7 @@ class StaticHTMLRendererTests(TestCase):
|
||||||
|
|
||||||
def test_static_renderer_with_exception(self):
|
def test_static_renderer_with_exception(self):
|
||||||
context = {
|
context = {
|
||||||
|
'view': APIView(),
|
||||||
'response': Response(status=500, exception=True),
|
'response': Response(status=500, exception=True),
|
||||||
'request': Request(HttpRequest())
|
'request': Request(HttpRequest())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user