views:

2455

answers:

9

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.
+3  A: 

Any particular reason you want to use Django's templates? Both Jinja and Genshi are, in my opinion, superior.


If you really want to, then see the Django documentation on settings.py. Especially the section "Using settings without setting DJANGO_SETTINGS_MODULE". Use something like this:

from django.conf import settings
settings.configure (FOO='bar') # Your settings go here
John Millikin
A: 

Google AppEngine uses the Django templating engine, have you taken a look at how they do it? You could possibly just use that.

William Keller
+1  A: 

Found this:

http://snippets.dzone.com/posts/show/3339

Lou Franco
+31  A: 

The solution is simple. It's actually well documented, but not too easy to find. (I had to dig around--it didn't come up when I tried a few different Google searches.) See http://docs.djangoproject.com/en/dev/ref/templates/api/#configuring-the-template-system-in-standalone-mode.

The following code works:

>>> from django.template import Template, Context
>>> from django.conf import settings
>>> settings.configure()
>>> t = Template('My name is {{ my_name }}.')
>>> c = Context({'my_name': 'Daryl Spitzer'})
>>> t.render(c)
u'My name is Daryl Spitzer.'

See the Django documentation for a description of some of the settings you may want to define (as keyword arguments to configure).

Daryl Spitzer
+17  A: 

Jinja2 syntax is pretty much the same as Django's with very few differences, and you get a much more powerfull template engine, which also compiles your template to bytecode (FAST!).

I use it for templating, including in Django itself, and it is very good. You can also easily write extensions if some feature you want is missing.

Here is some demonstration of the code generation:

>>> import jinja2
>>> print jinja2.Environment().compile('{% for row in data %}{{ row.name | upper }}{% endfor %}', raw=True) 
from __future__ import division
from jinja2.runtime import LoopContext, Context, TemplateReference, Macro, Markup, TemplateRuntimeError, missing, concat, escape, markup_join, unicode_join
name = None

def root(context, environment=environment):
    l_data = context.resolve('data')
    t_1 = environment.filters['upper']
    if 0: yield None
    for l_row in l_data:
        if 0: yield None
        yield unicode(t_1(environment.getattr(l_row, 'name')))

blocks = {}
debug_info = '1=9'
nosklo
I'm using Jinja in a project of mine, because I wanted something that I was fairly familiar with, but didn't want my users (since it's a distributable app) to have to install Django. A plus is that Jinja can be installed with easy_install.
Xiong Chiamiov
Django can be installed with easy_install as well.
hegemon
A: 

I echo the above statements. Jinja 2 is a pretty good superset of Django templates for general use. I think they're working on making the Django templates a little less coupled to the settings.py, but Jinja should do well for you.

Clint Ecker
+4  A: 

I would also recommend jinja2. There is a nice article on django vs. jinja2 that gives some in-detail information on why you should prefere the later.

olt
A: 

Don't. Use StringTemplate instead--there is no reason to consider any other template engine once you know about it.

Rob Williams
+1  A: 

I would say Jinja as well. It is definitely more powerful than Django Templating Engine and it is stand alone.

If this was an external plug to an existing Django application, you could create a custom command and use the templating engine within your projects environment. Like this;

manage.py generatereports --format=html

But I don't think it is worth just using the Django Templating Engine instead of Jinja.

muhuk