Question

My project has this middleware setting:

MIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
)

For local development, I setup a local_settings.py module which redefines the middleware setting as:

MIDDLEWARE_CLASSES = (
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)

Here I'm just removing the cache-related middleware, just for development.

In my Apache deploy, the local_settings.py only redefines DEBUG (to False) and the DATABASES dict.

I even installed IPython in the project's production virtualenv (the one that the Apache server uses) and executed this:

In [1]: from django.conf import settings

In [2]: settings.MIDDLEWARE_CLASSES
Out[2]: 
('django.middleware.cache.UpdateCacheMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
 'django.middleware.cache.FetchFromCacheMiddleware')

But, despite all this, I still have to enter a language identifier in the URL (as httpp://mysite.com/en/myview/) to access any URL that uses from django.conf.urls.i18n.i18n_patterns. And one of the functions of django.middleware.locale.LocaleMiddleware is to do precisely that, when accessing a localized view without a language code prefix, guess it from the user-agent headers and redirect to the prefixed view.

How to debug what's going on?

Was it helpful?

Solution

I figure it out (silly problem...) Looking at the code, django.middleware.locale.LocaleMiddleware first inspects if the response status code is 404 (Not-Found) and then retries the request with the language code prefixed. But, that code never gets executed... just because I didn't create the 404.html template, raising an exception, etc...

This also explains why it worked when DEBUG = True, because in debug mode the 404.html template is never used.

Just created the corresponding 404.html template and everything worked fine.

OTHER TIPS

I've just had the same problem, but : I had defined a handler_404 in my urlconf, pointing to a view. This view returned an HttpResponse, but with status_code = 200 instead of 404. Hence the same problem.

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