Skip to content

Instantly share code, notes, and snippets.

@flashingpumpkin
Created December 7, 2011 18:20
Show Gist options
  • Select an option

  • Save flashingpumpkin/1443948 to your computer and use it in GitHub Desktop.

Select an option

Save flashingpumpkin/1443948 to your computer and use it in GitHub Desktop.
Render only part of a django template
from django import template
tpl = template.Template("""
{% block head %}{% endblock %}
{% block body %}
{% block content %}{% endblock %}
{% endblock %}
""")
def getnode(node, name = 'content'):
if hasattr(node, 'name') and node.name == name:
return node
if not hasattr(node, 'nodelist'):
return None
for child in node.nodelist:
result = getnode(child, name)
if result is not None:
return result
def myhandler(request):
if request.is_ajax:
return getnode(tpl, 'content').render(template.Context({}))
return tpl.render(template.Context({}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment