Skip to content

Instantly share code, notes, and snippets.

@hirokiky
Created December 30, 2014 13:02
Show Gist options
  • Save hirokiky/3a9a8d40475b1122b72c to your computer and use it in GitHub Desktop.
Save hirokiky/3a9a8d40475b1122b72c to your computer and use it in GitHub Desktop.
Django View decorator to convert returned dictionary into TemplateResponse.
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