django-render 0.3.5

Django templates render sugar
django-render is a Django app that provides decorators for templates rendering and CBV mixin in request context with simple code use.

Important: all template renders goes in request context. Sessions, cookies, meta, etc is available from templates.

Usage in functional views

It support simple template rendering with decorator function. It is basic use example. Will be rendered template "APPNAME/VIEWNAME.html"

from render.template import render
@render
def index():
 #index view logic goes here
 return {
 'var1': val1,
 'var2': val2,
 }

from render.template import render
@render
def index():
 #index view logic goes here
 return {
 'var1': val1,
 'var2': val2,
 }, 'text/plain'

You can override VIEWNAME part in template path. See example.

from render.template import render
@render
def all():
 #all view logic goes here
 return 'index.html', {
 'var1': val1,
 'var2': val2,
 }


You can override APPNAME part in template path. See example.

from render.template import renderer
@renderer("otherapp")
def all():
 #all view logic goes here
 return 'index.html', {
 'var1': val1,
 'var2': val2,
 }


Or you can return "ready to use HttpResponse" object. render wrapepr just return it.

from render.template import render
@render
def all():
 if some_logic:
 return HttpResponse("It's ok too")
 #all view logic goes here
 return 'index.html', {
 'var1': val1,
 'var2': val2,
 }


Usage in Class Based Views

It supports basic TemplateView's like

class Index(RenderViewMixin, TemplateView):
 pass


It calculates template name as APP/VIEW.html

You can override heuristic by declaring template_name variable like

class Index(RenderViewMixin, TemplateView):
 template_name = 'custom.html'

This call APP/custom.html. Or you can add full template path like

class Index(RenderViewMixin, TemplateView):
 template_name = 'otherapp/custom.html'

Then will be called 'otherapp/custom.html'

Like functional view you can use render sugar in get/post/delete/etc request to your CBV.

class Index(RenderViewMixin, TemplateView):
 def get(self, request, *args, **kwargs):
 return {
 "title": 'My awesome title!'
 }


Supported all sugar with defining template name, context data and mimetype.

class Index(RenderViewMixin, TemplateView):
 def get(self, request, *args, **kwargs):
 return 'print.html', {
 "title": 'My awesome title!'
 }, 'text/plain'


It works and with global template_name defining.

class Index(RenderViewMixin, TemplateView):
 template_name = 'default.html'
 def get(self, request, *args, **kwargs):
 return {
 "title": 'My awesome title!'
 }, 'text/plain'


Template processing

Into template context render add few variables.

- App - Application name, where was called view
- View - View function name
- Layout - Base layout path. Compiles from APPNAME and base.html. Example: for news app it will be equal "news/base.html"

Example in template: {% extends Layout %}

< div class="{{ App }}_{{ View }}" >{% block content %}< /div >

It is clean & dry helper! Use it :-)

phpdude

last updated on:
May 1st, 2012, 12:11 GMT
price:
FREE!
developed by:
Alexandr I. Shurigin
license type:
Other/Proprietary License
category:
ROOT \ Internet \ HTTP (WWW)

FREE!

In a hurry? Add it to your Download Basket!

user rating

UNRATED
0.0/5
 

0/5

Add your review!

SUBMIT