Opened 18 years ago

Closed 18 years ago

#3687 closed (fixed)

django.template needs settings already configured at import time

Reported by: johan.tibell@… Owned by: Adrian Holovaty
Component: Template system Version: 0.95
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The import:

from django.conf import settings

Raises:

EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.

Which makes it impossible to use settings.configure() which as described http://www.djangoproject.com/documentation/templates_python/ under stand-alone mode.

Expected behaviour: It should be possible to use settings.configure() instead of DJANGO_SETTINGS_MODULE.

Change History (6)

comment:1 by Malcolm Tredinnick, 18 years ago

Can you provide a few more details on how you are triggering this error, please? I cannot repeat it; I can happily import django.conf.settings without seeing this error. My test is

.-(malcolm@counterweight 20:05:28) ~
`--> python
Python 2.4.3 (#1, Oct 23 2006, 14:19:45)
[GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.conf import settings
>>>

Does that work for you?

You will see the error if you try to do absolutely anything with settings before calling configure(), but a simple import before using it should work.

comment:2 by johan.tibell@…, 18 years ago

Apparently it was the template import that broke it.

>>> from django.conf import settings
>>> from django import template
EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.

Interleaving a configure() call fixed it:

>>> from django.conf import settings
>>> settings.configure(TEMPLATE_DIRS=('/home/username/templates'))
>>> from django import template

The docs could be a bit more explicit about what "using" code before calling configure() means. Calling code in the middle of imports is a bit unintuitive (and ugly, at least to me).

comment:3 by Malcolm Tredinnick, 18 years ago

Hmmm. That's a little unexpected. I thought that used to work.

For anybody looking at this...

There are some places where an import forces a setting to be accessed, but they shouldn't be very common. We have moved a lot of settings accesses out of default arguments in functions for just that reason. For example, have a lok at how we set up the default arguments in django.templates.loaders.filesystem.get_template_sources() -- we ensure settings is not accessed at import time. Ideally, imports should be safe from accessing settings, but executing any code at all means all bets are off.

In the interim, the workaround in Johan's previous comment is required.

comment:4 by Malcolm Tredinnick, 18 years ago

Triage Stage: UnreviewedAccepted

comment:5 by Malcolm Tredinnick, 18 years ago

Summary: django.conf.settings reads DJANGO_SETTINGS_MODULE at import timedjango.template needs settings already configured at import time

Changing the title to reflect the real problem.

comment:6 by Malcolm Tredinnick, 18 years ago

Resolution: fixed
Status: newclosed

(In [4905]) Delayed the reading of settings.USE_I18N until the first use of the i18n
functions. This solves a few import problems we are seeing. Fixed #3687. Refs #2920.

Note: See TracTickets for help on using tickets.
Back to Top