Created
December 30, 2014 13:02
-
-
Save hirokiky/3a9a8d40475b1122b72c to your computer and use it in GitHub Desktop.
Django View decorator to convert returned dictionary into TemplateResponse.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from django.template.response import TemplateResponse | |
def template_view(template_name): | |
""" View decorator to convert returned dictionary into TemplateResponse | |
You can apply template name for this decorator like this | |
>>> @template_view | |
>>> def page_detail(request): | |
>>> return {'page_title': 'Some Page Title'} # This will automatically used as context. | |
If you return some object excluding dictionary, `template_view` will return it directly. | |
""" | |
def decorator(view_func): | |
@wraps(view_func) | |
def wrapped(request, *args, **kwargs): | |
response = view_func(request, *args, **kwargs) | |
if isinstance(response, dict): | |
return TemplateResponse(request, template_name, context=response) | |
else: | |
return response | |
return wrapped | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment