Django is “order sensitive”

Follow me for more content or contact for work opportunities:
Twitter / LinkedIn

Sometimes I forget that django's settings.py is a Python script, and not a plain configuration file. And forgetting it causes django to behave unexpectedly. A couple of examples that happened to are related to array sorting.

Some days ago I customized middleware classes, and after that I left on my setting.py:

MIDDLEWARE_CLASSES = (
'django.middleware.locale.LocaleMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.middleware.common.CommonMiddleware',
)

With it, LocaleMiddleware doesn't work, because it requires SessionMiddleware that isn't loaded when LocaleMiddleware is executed.

Today's issue was something similar, but with templates. I customized some admin templates, copying them to a directory loaded with filesystem loader. My settings.py looked like:

TEMPLATE_LOADERS = (
'django.template.loaders.app_directories.load_template_source',
'django.template.loaders.filesystem.load_template_source',
)

With it, loaders looked first to application template directories, including the admin ones, so overriding template was never used.

Follow me for more content or contact for work opportunities:
Twitter / LinkedIn