Login

Renderer decorator

Author:
GaretJax
Posted:
November 28, 2007
Language:
Python
Version:
.96
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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.