Opened 18 years ago
Closed 18 years ago
#3687 closed (fixed)
django.template needs settings already configured at import time
Reported by: | 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 , 18 years ago
comment:2 by , 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 , 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 , 18 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:5 by , 18 years ago
Summary: | django.conf.settings reads DJANGO_SETTINGS_MODULE at import time → django.template needs settings already configured at import time |
---|
Changing the title to reflect the real problem.
comment:6 by , 18 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
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 isDoes 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.