Skip to content

Instantly share code, notes, and snippets.

@iomarmochtar
Created July 19, 2017 05:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iomarmochtar/e081845120c1e1aeb9f29e39050f5365 to your computer and use it in GitHub Desktop.
Save iomarmochtar/e081845120c1e1aeb9f29e39050f5365 to your computer and use it in GitHub Desktop.
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