在生产环境中输入 url mysite.com 得到一个 `HTTP/1.1" 500 5837` - i18n

Typing url mysite.com get a `HTTP/1.1" 500 5837` in production - i18n

输入 www.mysite.com 在生产中得到 HTTP/1.1" 500 5837 如果我输入 www.mysite.comDEBUG = False 它会被重定向到 www.mysite.com/de/ 一切正常。

为什么会这样?

注意:我使用的是 Django 1.8、Apache,mod_wsgi

settings.py

DEBUG = False
ALLOWED_HOSTS = ['.mysite.com', 'mysite.com.']
MIDDLEWARE_CLASSES = (
  'django.contrib.sessions.middleware.SessionMiddleware',
  'django.middleware.common.CommonMiddleware',
  'django.middleware.csrf.CsrfViewMiddleware',
  'django.contrib.auth.middleware.AuthenticationMiddleware',
  'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
  'django.contrib.messages.middleware.MessageMiddleware',
  'django.middleware.clickjacking.XFrameOptionsMiddleware',
  'django.middleware.security.SecurityMiddleware',
  'django.middleware.locale.LocaleMiddleware',
)

ROOT_URLCONF = 'web.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates/'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'web.wsgi.application'

LANGUAGE_CODE = 'de-de'

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGES = (
    ('en', _('English')),
    ('de', _('German')),
)


TIME_ZONE = 'CET'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

LOCALE_PATHS = (
    os.path.join(BASE_DIR, "locale/"),
)

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(BASE_DIR, "static/"),
)

urls.py

from django.conf.urls import url, include
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin

from . import views

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
]

urlpatterns += i18n_patterns(
    url(r'^$', views.home, name='home'),
    ...
)

这是 DEBUG = False

时的堆栈跟踪
(venv)localhost:app-do user$ ./manage.py runserver 0.0.0.0:7000
Performing system checks...

System check identified no issues (0 silenced).
September 28, 2015 - 18:51:51
Django version 1.8.4, using settings 'web.settings'
Starting development server at http://0.0.0.0:7000/
Quit the server with CONTROL-C.
[28/Sep/2015 18:51:55] "GET / HTTP/1.1" 500 5859
- Broken pipe from ('127.0.0.1', 53604)
[28/Sep/2015 18:51:55] "GET / HTTP/1.1" 500 5859

这里是 DEBUG = True

^C(venv)localhost:app-do user$ ./manage.py runserver 0.0.0.0:7000
Performing system checks...

System check identified no issues (0 silenced).
September 28, 2015 - 18:52:17
Django version 1.8.4, using settings 'web.settings'
Starting development server at http://0.0.0.0:7000/
Quit the server with CONTROL-C.
[28/Sep/2015 18:52:21] "GET / HTTP/1.1" 302 0
[28/Sep/2015 18:52:21] "GET /en/ HTTP/1.1" 200 8630

答案如下: setting DEBUG to False change behavior of i18n

The problem here is that you probably have no 404.html in your template folder, hence the code path is generating a server error (500) when DEBUG is False. You could probably have seen an appropriate message in your server's logs.

使用这个 404.html 模板检查一下:

{% extends "base.html" %}

{% block title %}Page not found{% endblock %}

{% block content %}
<h1>Page not found</h1>

<p>Sorry, but the requested page could not be found.</p>
{% endblock %}