1

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?

4

2 回答 2

3

我想通了(愚蠢的问题......)查看代码,django.middleware.locale.LocaleMiddleware首先检查响应状态代码是否为404(未找到),然后使用前缀语言代码重试请求。但是,该代码永远不会被执行......只是因为我没有创建404.html模板,引发异常等等......

这也解释了为什么它在 时起作用DEBUG = True,因为在调试模式下404.html从不使用模板。

刚刚创建了相应的404.html模板,一切正常。

于 2012-07-14T19:52:10.877 回答
2

我刚刚遇到了同样的问题,但是:我handler_404在我的 urlconf 中定义了一个,指向一个视图。这个视图返回了一个 HttpResponse,但status_code = 200不是 404。因此同样的问题。

于 2012-10-31T23:04:12.990 回答