How do I use Django templates without the rest of Django

0 votes

I want to use the Django template engine in my (Python) code, but I'm not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?

If I run the following code:

>>> import django.template
>>> from django.template import Template, Context
>>> t = Template('My name is {{ my_name }}.')

I get:

ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

Jun 23, 2020 in Python by kartik
• 37,520 points
1,580 views

1 answer to this question.

0 votes

Hello @kartik,

Let's say you have this important template tag in the module read.py

from django import template

register = template.Library()

@register.filter(name='bracewrap')
def bracewrap(value):
    return "{" + value + "}"

This is the html template file "temp.html":

{{var|bracewrap}}

Finally, here is a Python script that will tie to all together

import django
from django.conf import settings
from django.template import Template, Context
import os

#load your tags
from django.template.loader import get_template
django.template.base.add_to_builtins("read")

# You need to configure Django a bit
settings.configure(
    TEMPLATE_DIRS=(os.path.dirname(os.path.realpath(__file__)), ),
)

#or it could be in python
#t = Template('My name is {{ my_name }}.')
c = Context({'var': 'edureka.co'})

template = get_template("temp.html")
# Prepare context ....
print template.render(c)

The output would be

{edureka.co}

Hope it helps!!

answered Jun 23, 2020 by Niroj
• 82,840 points

Related Questions In Python

+1 vote
1 answer

How do I use the data of an array after splitting it?

Hey, You can use  np.greater(b, f) np.greater_equal(b, f) np.less(b, f) np.less_equal(b, ...READ MORE

answered Dec 9, 2019 in Python by Payal
748 views
0 votes
1 answer

In NumPy how do I get the maximum of subsets? Python

You can use np.maximum.reduceat: >>> _, idx = np.unique(g, ...READ MORE

answered Nov 9, 2018 in Python by Nymeria
• 3,560 points
1,621 views
+1 vote
1 answer
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
2,505 views
0 votes
1 answer

How to do math in a Django template?

Hello @kartik, You can use the add filter: {{ object.article.rating_score|add:"-100" }} Thank ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
6,556 views
0 votes
1 answer

How do I add a link from the Django admin page of one object to the admin page of a related object?

Hello @kartik, Set show_change_link to True (False by default) in your inline ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,840 points
10,653 views
0 votes
1 answer

How do I use the built in password reset/change views with my own templates?

Hello @kartik, You'll notice that password_reset takes a named parameter ...READ MORE

answered Aug 13, 2020 in Python by Niroj
• 82,840 points
1,399 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP