# coding:utf-8 ''' Created on 2009-12-30 @author: Jason Green @author-email: guileen@gmail.com in settings.py: JINJA_EXTS=('jinja2.ext.i18n','youproject.app.extensions.csrf_token',) use in jinja2 template just like django template:
''' from jinja2 import nodes from jinja2.ext import Extension from django.utils.safestring import mark_safe import traceback class CsrfExtension(Extension): # a set of names that trigger the extension. tags = set(['csrf_token']) def __init__(self, environment): self.environment = environment def parse(self, parser): try: token = parser.stream.next() return nodes.Output([self.call_method('_render', [nodes.Name('csrf_token','load')])]).set_lineno(token.lineno) except: traceback.print_exc() def _render(self, csrf_token): """Helper callback.""" if csrf_token: if csrf_token == 'NOTPROVIDED': return mark_safe(u"") else: return mark_safe(u"" % (csrf_token)) else: # It's very probable that the token is missing because of # misconfiguration, so we raise a warning from django.conf import settings if settings.DEBUG: import warnings warnings.warn("A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext.") return u'' csrf_token=CsrfExtension