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

时间:2021-10-30 19:21:28

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/

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

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

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

I saw this question, which is close to my scenario

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

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

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

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

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

Below is my code

以下是我的代码

settings.py

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

P.S我正在使用django 1.5

1 个解决方案

#1


9  

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

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

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

should solve your problem (have had it before).

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

#1


9  

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

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

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

should solve your problem (have had it before).

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