Django url internationalization not working in production mode

陌路散爱 提交于 2019-12-13 01:34:51

问题


i'm programming in django 1.4 a site with multilanguage support (french and english).

Everything works fine in debug mode. Then I pass to production mode (DEBUG=False) and the urls no longer work.

Ex. : in debug mode, when I request the page /agricole/, django redirect automatically to /fr/agricole.

But in production mode, it sends a error and no longer redirect the page.

I have searched the web, check the LocaleMiddleware, like said on django documentation and everything, but it still fails.

I cannot hard code /fr/* in the templates, since it will break the multilingue support. And writting by hand a redirector who redirect, is not clean.

What do I miss? A template tag? a url config?

My

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'cms.middleware.page.CurrentPageMiddleware',
    'cms.middleware.user.CurrentUserMiddleware',
    'cms.middleware.toolbar.ToolbarMiddleware',
    'cms.middleware.language.LanguageCookieMiddleware',
)

and my url.py

from django.conf.urls.defaults import *
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.conf import settings
from django.views.generic import RedirectView

admin.autodiscover()

import annonces.views as annonces

urlpatterns = i18n_patterns('',
    url(r'.*register/$', 'annonces.register', name='register'),
    url(r'^admin/', include(admin.site.urls)),
    url(r'accounts/profile/$', RedirectView.as_view(url='/')),
    url(r'^favicon\.ico$', RedirectView.as_view(url='static/favicon.ico')),
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
    (r'^accounts/', include('registration.backends.simple.urls')),
    (r'^tinymce/', include('tinymce.urls')),
    url(r'^longlat/(.*)$', 'annonces.longlat', name='longlat'),
    url(r'^', include('cms.urls')),
)

回答1:


I found problem of this issue. if you have some middlewares that handle 404 page, then this page must retur status 404. If it will return othe status then i18n redirect will not work.



来源:https://stackoverflow.com/questions/25794161/django-url-internationalization-not-working-in-production-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!