Over a million developers have joined DZone.

Bootstrap Django Templates Out-of-framework

·
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'))
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}