Question

I'm migrating a Google App Engine application from Django 0.96 to 1.2. In the process, I noted that the settings file I specified in my main handler was not loaded anymore (instead of the 3 specified languages, Django loaded the full list of default languages from it's django/conf/global_settings.py file).

In 0.96 I specified the file like this:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'conf.settings'

From what I read in the documentation this is not supposed to be changed. However, when specifying version 1.2 like this:

from google.appengine.dist import use_library
use_library('django', '1.2')

conf.settings is not imported anymore. Digging a bit in the GAE's SDK, I noted that the 0.96 and 1.2 versions of the google_appengine/lib/django_<VERS>/django/conf/__init__.py file work differently.

In 0.96, LazySettings._import_settings gets called from LazySettings.__getattr__, which results in the file specified in os.environ['DJANGO_SETTINGS_MODULE'] to be imported.

In 1.2, LazySettings._setup does not seem to be called anywhere.

If I add a manual call to settings._setup() in my main handler, the custom settings file gets imported properly. Like this:

from django.conf import settings
settings._target = None # Force Django to reload settings
settings._setup()

However, it feels weird/wrong to have to call that private function manually... Why is my custom settings file not imported anymore?

Was it helpful?

Solution

There's a good explanation of what's going on here:

Django settings are loaded from the specified settings.py lazily, the first time they are accessed. The problem is that importing webapp.template calls django.conf.settings.configure() to attempt to set up some settings. Therefore if webapp.template is imported before any settings are accessed, then settings.py is never loaded (as the settings accessor finds that settings already exist, and doesn't attempt to load any more).

The solution to this is to force an access to the settings, to load the settings.py, before webapp.template is imported. Then when webapp.template is later imported, its call to django.conf.settings.configure() is ignored.

Sounds like a bug to me. You could submit it to the issue tracker.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top