Snippet List
init env
`env = Envoriment(extensions=('youproject.app.extensions.csrf_token'), loader=loader)`
or see [http://www.djangosnippets.org/snippets/1844/] and in settings.py:
`JINJA_EXTS=('jinja2.ext.i18n','youproject.app.extensions.csrf_token',)`
use this extension in jinja2 template just like django template:
`<form ...>{% csrf_token %}...</form>`
manything need to do with RequestContext, but it's too tedious.
use
`render_to_response("/my.html", {'key':value,},request)`
instead of
`render_to_response("/my.html", {'key':value,},new RequestContext(request)) `
and you can also use
`render_to_response("/my.html", {'key':value,},new RequestContext(request))`
I tried a few snippets of integrated jinja2 in django, which provided ?.render_to_string and ?.render_to_response in the way of jinja2. **But those snippets could not use the generic view**, because of the generic views is use default django template. so i write this snippet which could use generic view, and use the basic django.shortcuts.render_to_string, django.shortcuts.render_to_string.
#in yourproject/urls.py :
#install default environment is very simple
djangojinja2.install()
#install environment of specified Environment class
from jinja2.sandbox import SandboxedEnvironment
djangojinja2.install(SandboxedEnvironment)
#alternative you can install sepcified environment
env=Environment(...)
...
djangojinja2.install(env)
#something could be set in settings.py
TEMPLATE_DIRS = (path.join(path.dirname(__file__),"templates"),)
JINJA_GLOBALS=['myapp.myutil.foo',]
JINJA_FILTERS=['django.template.defaultfilters.date',]
JINJA_TESTS=('foo.mytest',)
JINJA_EXTS=['jinja2.ext.i18n']
#there is no change need for app.views
from django.shortcuts import render_to_response
def foo(request):
return render_to_response('/myjinja2.html',{'request':request})
test in django development version of r12026 , jinja2 2.2.1, python 2.5
- template
- generic
- jinja2
- generic-view
jasongreen has posted 4 snippets.