Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Django Template Outside Django Project
import django
from django.template import Template, Context
from django.conf import settings
# optional if you just render str instead of template file
from django.template.loader import get_template
settings.configure(TEMPLATES=[{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# if you want to render using template file
'DIRS': ['/tmp/template_dirs']
}])
django.setup()
# variables that will be passed to template
vars = {'name':'mochtar'}
print Template("Using string = {{name}}").render(Context(vars))
# file say_hello.tmpl located in folder /tmp/template_dirs as it's configured above
print get_template("say_hello.tmpl").render(vars)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment