Search code examples
pythondjangoapachehttpdjango-i18n

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


Typing www.mysite.com get a HTTP/1.1" 500 5837 in production and if I type www.mysite.com with DEBUG = False it gets redirected to www.mysite.com/de/ and everything works as normal.

Why is this happening?

Note: I'm using 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'),
    ...
)

Here is the stack trace when 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

And here for 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

Solution

  • Here is the answer: 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.

    Use this 404.html template to check this out:

    {% 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 %}