Django locale enabled app always returns 404 when debug is off

蓝咒 提交于 2019-12-12 11:44:52

问题


I've been developing an app that uses i18n/locale as url prefix in the root/landing page. I tried to deploy my application earlier for testing purposes with DEBUG=True and everything was working perfectly. If attempting to access domain.com it would redirect to domain.com/en/

Now, since I disabled DEBUG=False, it won't redirect to /en/, instead it would show 404 error.

I saw this question, which is close to my scenario

Django 1.4 LocaleMiddleware not working with Apache, but works with runserver

but, for my case, I already have the 404 page setup and its handlers and 404.html file.

Below is my code

settings.py

LANGUAGE_CODE = 'en-us'
ADMIN_LANGUAGE_CODE = LANGUAGE_CODE

gettext = lambda s: s

LANGUAGES = (
    ('ar', gettext('Arabic')),
    ('en', gettext('English')),
)

USE_I18N = True

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',
    'apps.accounts.middleware.AccountSocialAuthExceptionMiddleware',
    'apps.core.middleware.DefaultSiteMiddleware',
    #    'debug_toolbar.middleware.DebugToolbarMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = (

    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.request",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
    # custom context processors
    "snowflake.apps.core.context_processors.project_name",
    "snowflake.apps.core.context_processors.sites",


)

and here is my URLs

from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib.staticfiles.urls import staticfiles_urlpatterns


urlpatterns = patterns('',
                       url(r'^admin/', include('apps.mod.urls')),
                       url(r'^redirect/login/$', 'apps.core.views.redirect_to_accounts', name='login-redirect'),
)

urlpatterns += i18n_patterns('',

                             url(r'^$', 'apps.publication.views.index', name='home'),

                             url(r'^publication(s)?/', include('apps.publication.urls')),

                             url(r'^comment(s)?/', include('apps.comment.urls')),

                             url(r'^rating(s)?/', include('apps.rating.urls')),

                             url(r'^tag(s)?/', include('apps.taggable.urls')),

                             url(r'^search/', include('apps.search.urls')),

                             url(r'^guard(s)?/', include('apps.guard.urls')),
)

handler404 = 'apps.core.views.handler404'

P.S am using django 1.5


回答1:


Your view apps.core.view.handler404 returns a HttpResponse object... but its HTTP status code is 200.

response = render_to_response("...")
response.status_code = 404
return response

should solve your problem (have had it before).



来源:https://stackoverflow.com/questions/16561225/django-locale-enabled-app-always-returns-404-when-debug-is-off

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