render_to

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def render_to(template):
    """
    Decorator for Django views that sends returned dict to render_to_response function
    with given template and RequestContext as context instance.

    If view doesn't return dict then decorator simply returns output.
    Additionally view can return two-tuple, which must contain dict as first
    element and string with template name as second. This string will
    override template name, given as parameter

    Parameters:

     - template: template name to use
    """
    def renderer(func):
        def wrapper(request, *args, **kw):
            output = func(request, *args, **kw)
            if isinstance(output, (list, tuple)):
                return render_to_response(output[1], output[0], RequestContext(request))
            elif isinstance(output, dict):
                return render_to_response(template, output, RequestContext(request))
            return output
        return wrapper
    return renderer

More like this

  1. render_with decorator by tobias 5 years, 7 months ago
  2. Render Decorator by dahool 4 years, 7 months ago
  3. Auto rendering decorator with options by Batiste 6 years, 2 months ago
  4. Render (with RequestContext) by alcides 5 years, 8 months ago
  5. Convert LaTeX templates to various output formats by blizz 7 years, 1 month ago

Comments

alfor (on April 6, 2010):

wonderful snippet, this should be in django.shortcuts

#

(Forgotten your password?)