Skip to content

Instantly share code, notes, and snippets.

@0atman
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0atman/1912bfd62f8dbd93cfa5 to your computer and use it in GitHub Desktop.
Save 0atman/1912bfd62f8dbd93cfa5 to your computer and use it in GitHub Desktop.
A decorator to render the view with a template
from django.shortcuts import render_to_response
def with_template(template):
"""
A decorator to render the view with a template. The view just
returns a contect dictionary. eg:
@with_template('index.html')
def index_view(request):
return {"name": "dave"}
"""
def template_decorator(view):
def view_wrapper(*args, **kwargs):
return render_to_response(
template,
view(*args, **kwargs)
)
return view_wrapper
return template_decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment