Skip to content
Jun 20 10

Accept template tags in Django Flatpages

by admin

You need to use template tags in Django FlatPages application ?

Here is a solution:

First create an file in your templatetags directory with the following content:

from django import template

register = template.Library()

def do_evaluate(parser, token):
    tag usage {% evaluate object.textfield %}
        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):
            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"
<title>{{ flatpage.title }}</title>
{% load evaluate_tag %}
{% evaluate flatpage.content %}

Now, you can use template tags in your flatpages !

Jun 8 10

Django 1.2.1 – CSRF verification failed – 403 Error

by admin

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.

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 file.


May 20 10

Django 1.2 just released

by admin

Here are the new features.

Apr 29 10

Welcome to Django Dev

by admin

Just a new blog about Django development : generic code, test driven development, internationalization…