Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
[Django 1.3] Template tag that allows Django tags in variables (i.e. flatpage content).
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
def allow_tags(parser, token):
Example: {% allow_tags page.content %}
# Splitting by None == splitting by spaces:
tag_name, var_name = token.contents.split(None, 1)
except ValueError:
raise template.TemplateSyntaxError, '%r tag requires arguments' % token.contents.split()[0]
return RenderNode(var_name)
allow_tags.is_safe = True
class RenderNode(template.Node):
def __init__(self, content):
self.content = content
def render(self, context):
content = template.Variable(self.content).resolve(context)
return template.Template(content).render(template.Context(context, autoescape=False))
except template.TemplateSyntaxError, e:
return mark_safe('Template error: There is an error one of this page\'s template tags: <code>%s</code>' % e.message)
{% extends "base.html" %}
{% load allow_tags %}
{% allow_tags page.content %}
Copy link

surdu commented Aug 7, 2019

@mhulse FYI: change my website, so my article is now at

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment