Created
July 19, 2017 05:47
-
-
Save iomarmochtar/e081845120c1e1aeb9f29e39050f5365 to your computer and use it in GitHub Desktop.
Using Django Template Outside Django Project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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