启用Django语言环境的应用程序在调试关闭时始终返回404

时间:2013-05-15 09:19:29

标签: django django-1.5

我一直在开发一个使用i18n / locale作为根/登录页面中的url前缀的应用程序。我尝试早期部署我的应用程序以进行测试,DEBUG = True,一切都运行良好。如果尝试访问domain.com,则会重定向到domain.com/en /

现在,由于我禁用了DEBUG = False,它不会重定向到/ en /,而是会显示404错误。

我看到了这个问题,这与我的情景很接近

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

但是,对于我的情况,我已经有404页面设置及其处理程序和404.html文件。

以下是我的代码

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",


)

这是我的网址

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 am django 1.5

1 个答案:

答案 0 :(得分:10)

您的观点apps.core.view.handler404会返回HttpResponse个对象...但其HTTP状态代码为200.

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

应该解决你的问题(以前有过)。