5

我一直在开发一个在根/登陆页面中使用 i18n/locale 作为 url 前缀的应用程序。我尝试使用 DEBUG=True 更早地部署我的应用程序以进行测试,并且一切正常。如果尝试访问 domain.com,它将重定向到 domain.com/en/

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

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

Django 1.4 LocaleMiddleware 不能与 Apache 一起使用,但可以与 runserver 一起使用

但是,就我而言,我已经有了 404 页面设置及其处理程序和 404.html 文件。

下面是我的代码

设置.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'

PS我正在使用django 1.5

4

1 回答 1

13

您的视图apps.core.view.handler404返回一个HttpResponse对象......但它的 HTTP 状态代码是 200。

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

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

于 2013-05-15T13:35:01.463 回答