Accept template tags in Django Flatpages
You need to use template tags in Django FlatPages application ?
Here is a solution:
First create an evaluate_tag.py file in your templatetags directory with the following content:
from django import template
register = template.Library()
@register.tag(name="evaluate")
def do_evaluate(parser, token):
"""
tag usage {% evaluate object.textfield %}
"""
try:
tag_name, variable = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0]
return EvaluateNode(variable)
class EvaluateNode(template.Node):
def __init__(self, variable):
self.variable = template.Variable(variable)
def render(self, context):
try:
content = self.variable.resolve(context)
t = template.Template(content)
return t.render(context)
except template.VariableDoesNotExist, template.TemplateSyntaxError:
return 'Error rendering', self.variable
Next, modifiy your flatpages template file to use this template tag:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>{{ flatpage.title }}</title>
</head>
<body>
{% load evaluate_tag %}
{% evaluate flatpage.content %}
</body>
</html>
Now, you can use template tags in your flatpages !
Here is the kind of error you can get while attempting to make a POST request after migrating to Django 1.2.1:
403 Forbidden
CSRF verification failed. Request aborted.
Help
Reason given for failure:
CSRF cookie not set.
To correct this, all you have to do is to add ‘django.middleware.csrf.CsrfViewMiddleware’, and ‘django.middleware.csrf.CsrfResponseMiddleware’ to your MIDDLEWARE_CLASSES in your settings.py file.
MIDDLEWARE_CLASSES = (
...
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
...
)
Django 1.2 just released
Here are the new features.
Welcome to Django Dev
Just a new blog about Django development : generic code, test driven development, internationalization…
