Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Bootstrap Django templates out-of-framework (See related posts)

This is the quickest way I've found to bootstrap Django templates without using the entire framework. This is suited for "plain-python" apps without using all the Django types. You'll probably want to cache the Template returns for each given name, but here's the basics.

# Kick off django config machinery first
from django.conf import settings
settings.configure(TEMPLATE_DIRS=("/whatever/templates",))

import django.template
import django.template.loader


def render(name, *values):
    ctx = django.template.Context()
    for d in values:
        ctx.push()
        ctx.update(d)

    t = django.template.loader.get_template(name)
    return t.render(ctx)

print render('layout.tmpl', dict(title='User'), dict(name='Bob', gender='M'))

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts