Login

render_with decorator

Author:
tobias
Posted:
September 4, 2008
Language:
Python
Version:
1.0
Tags:
render_to_response requestcontext decorator rendering
Score:
1 (after 3 ratings)

Automatically render your view using render_to_response with the given template name and context, using RequestContext (if you don't know what this is you probably want to be using it). For example:

@render_with('books/ledger/index.html')
def ledger_index(request):
    return {
        'accounts': ledger.Account.objects.order_by('number'),
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from django.shortcuts import render_to_response
from django.template import RequestContext

def render_with(template):
    def render_with_decorator(view_func):
        def wrapper(*args, **kwargs):
            request = args[0]
            context = view_func(*args, **kwargs)
            return render_to_response(
                template, 
                context, 
                context_instance=RequestContext(request),
            )
        return wrapper
    return render_with_decorator

More like this

  1. render_to by asolovyov 7 years, 1 month ago
  2. simplified render_to_response with RequestContext by jasongreen 5 years, 7 months ago
  3. Render (with RequestContext) by alcides 7 years ago
  4. Auto rendering decorator with options by Batiste 7 years, 6 months ago
  5. DRY template rendering decorator update by jakecr 5 years, 5 months ago

Comments

Please login first before commenting.