Login

Renderer decorator

Author:
GaretJax
Posted:
November 28, 2007
Language:
Python
Version:
.96
Tags:
shortcut render
Score:
0 (after 0 ratings)

You can use this as a decorator for your views, this way you can return a simple dictionary and the decorator will do the rest.

@TODO: Automatically set the template based on the app/view request

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from django.shortcuts import render_to_response

def render(template):
    def render_view(view):
        def data_to_render(request, *args, **keys):
            return render_to_response(template.replace('.', '/') + '.html', view(request, *args, **keys))
        return data_to_render    
    return render_view

# Example:
# @render('blog.article_list')         # The template to use
# def article_list(request):
#     return {                                      # The data to render
#         'article': Article.objects.all(),
#         'url': 'http://garetjax.info/',
#     }

More like this

  1. DRY template rendering decorator update by jakecr 4 years, 12 months ago
  2. render_to by asolovyov 6 years, 8 months ago
  3. Partial JSON template rendering by barbuza 7 years, 4 months ago
  4. Auto rendering decorator with options by Batiste 7 years, 1 month ago
  5. Render Decorator by dahool 5 years, 5 months ago

Comments

Please login first before commenting.