当找不到语言时,Django将错误500重定向到404

时间:2015-12-01 11:28:43

标签: python django django-cms

我注意到,当在settings.py中引用一种语言而网站上没有正确的页面时,它会生成500页。

例如:

    LANGUAGES = (
    ('en', gettext('English')),
    ('fr', gettext('French')),
    ('it', gettext('Italian')),
    ('es', gettext('Spanish')),
    ('de', gettext('German')),
    ('el', gettext('Greek')),
    ('tr', gettext('Turkish')),
    ('ja', gettext('Japanese')),
    ('pt', gettext('Portuguese')),
    ('ar', gettext('Arabic')),
)

因此,在我的网站上,en fr es deja已翻译并出现在网站上。其他人不是。 然后,如果你试图达到:example.org/el/whatever它引发500错误。

我想有一个404,以便让用户进入网站。

有没有人对这个问题有所了解?或者我是否必须从设置中删除语言?

非常感谢。

编辑:这是我遇到此错误的报告:

 Internal Server Error: /it/blog/
Traceback (most recent call last):
  File "/var/www/webapps/lib/python3.4/site-packages/cms/views.py", line 34,
 in _handle_no_page
    resolve('%s$' % request.path)
  File
 "/var/www/webapps/lib/python3.4/site-packages/django/core/urlresolvers.py", line 494, in resolve
    return get_resolver(urlconf).resolve(path)
  File
 "/var/www/webapps/lib/python3.4/site-packages/django/core/urlresolvers.py", line 358, in resolve
    raise Resolver404({'tried': tried, 'path': new_path})
django.core.urlresolvers.Resolver404: {'tried': [[<RegexURLResolver <module
 'autocomplete_light.urls' from
 '/var/www/webapps/lib/python3.4/site-packages/autocomplete_light/urls.py'> (None:None) ^autocomplete/>],
 [<RegexURLResolver <module 'djangocms_contact.urls' from
 '/var/www/webapps/proj_website/app/djangocms_plugins/djangocms_contact/urls.py'> (None:None)
 ^api/contact_plugin/>], [<RegexURLResolver <module 'taggit_autosuggest_select2.urls' from
 '/var/www/webapps/lib/python3.4/site-packages/taggit_autosuggest_select2/urls.py'>
 (None:None) ^taggit_autosuggest_select2/>], [<RegexURLPattern None
 ^sitemap\.xml$>], [<RegexURLPattern None ^sitemap-(?P<section>.+)\.xml$>],
 [<LocaleRegexURLResolver <RegexURLPattern list> (None:None) ^it/>, <RegexURLPattern
 None ^share/>], [<LocaleRegexURLResolver <RegexURLPattern list> (None:None)
 ^it/>, <RegexURLResolver <module 'djangocms_job.urls' from
 '/var/www/webapps/proj_website/app/djangocms_plugins/djangocms_job/urls.py'> (None:None)
  ^api-job_plugin/>], [<LocaleRegexURLResolver <RegexURLPattern list>
 (None:None) ^it/>, <RegexURLResolver <RegexURLPattern list> (admin:admin)
 ^admin/>], [<LocaleRegexURLResolver <RegexURLPattern list> (None:None) ^it/>,
 <RegexURLResolver <module 'cms.urls' from
 '/var/www/webapps/lib/python3.4/site-packages/cms/urls.py'> (None:None) ^>, <RegexURLPattern pages-details-by-slug
 ^(?P<slug>[0-9A-Za-z-_.//]+)/$>], [<LocaleRegexURLResolver <RegexURLPattern
 list> (None:None) ^it/>, <RegexURLResolver <module 'cms.urls' from
 '/var/www/webapps/lib/python3.4/site-packages/cms/urls.py'> (None:None) ^>,
 <RegexURLPattern pages-root ^$>], [<RegexURLResolver <module 'rosetta.urls' from
 '/var/www/webapps/lib/python3.4/site-packages/rosetta/urls.py'> (None:None)
 ^rosetta/>]], 'path': 'it/blog/$'}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File
 "/var/www/webapps/lib/python3.4/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/var/www/webapps/lib/python3.4/site-packages/cms/views.py", line
 118, in details
    return _handle_no_page(request, slug)
  File "/var/www/webapps/lib/python3.4/site-packages/cms/views.py", line 38,
 in _handle_no_page
    raise exc
django.http.response.Http404: {'tried': [[<RegexURLResolver <module
 'autocomplete_light.urls' from
 '/var/www/webapps/lib/python3.4/site-packages/autocomplete_light/urls.py'> (None:None) ^autocomplete/>], [<RegexURLResolver
 <module 'djangocms_contact.urls' from
 '/var/www/webapps/proj_website/app/djangocms_plugins/djangocms_contact/urls.py'> (None:None) ^api/contact_plugin/>],
 [<RegexURLResolver <module 'taggit_autosuggest_select2.urls' from
 '/var/www/webapps/lib/python3.4/site-packages/taggit_autosuggest_select2/urls.py'>
 (None:None) ^taggit_autosuggest_select2/>], [<RegexURLPattern None
 ^sitemap\.xml$>], [<RegexURLPattern None ^sitemap-(?P<section>.+)\.xml$>],
 [<LocaleRegexURLResolver <RegexURLPattern list> (None:None) ^it/>, <RegexURLPattern None
 ^share/>], [<LocaleRegexURLResolver <RegexURLPattern list> (None:None)
 ^it/>, <RegexURLResolver <module 'djangocms_job.urls' from
 '/var/www/webapps/proj_website/app/djangocms_plugins/djangocms_job/urls.py'> (None:None)
 ^api-jo
 b_plugin/>], [<LocaleRegexURLResolver <RegexURLPattern list> (None:None)
 ^it/>, <RegexURLResolver <RegexURLPattern list> (admin:admin) ^admin/>],
 [<LocaleRegexURLResolver <RegexURLPattern list> (None:None) ^it/>,
 <RegexURLResolver <module 'cms.urls' from
 '/var/www/webapps/lib/python3.4/site-packages/cms/urls.py'> (None:None) ^>, <RegexURLPattern pages-details-by-slug
 ^(?P<slug>[0-9A-Za-z-_.//]+)/$>], [<LocaleRegexURLResolver <RegexURLPattern list>
 (None:None) ^it/>, <RegexURLResolver <module 'cms.urls' from
 '/var/www/webapps/lib/python3.4/site-packages/cms/urls.py'> (None:None) ^>, <RegexURLPattern
 pages-root ^$>], [<RegexURLResolver <module 'rosetta.urls' from
 '/var/www/webapps/lib/python3.4/site-packages/rosetta/urls.py'> (None:None) ^rosetta/>]],
 'path': '/it/blog/'}

它继续这样(我有两个更大的paragraphes)开头:

Traceback (most recent call last):
  File "/var/www/webapps/lib/python3.4/site-packages/cms/views.py", line 34,

和:

Traceback (most recent call last):
  File
 "/var/www/webapps/lib/python3.4/site-packages/django/core/handlers/base.py", line 150, in get_response
    response = callback(request, **param_dict)
  File "/var/www/webapps/proj_website/app/proj/views.py", line 84, in
 custom_page_not_found

1 个答案:

答案 0 :(得分:0)

您的views.py似乎出现了问题。

因此,您需要在视图中执行以下问题:

from django.http import Http404

def your_view(request):
    try:
       code_that_must_work_but_can_fail="goes here"
    except Exception, e:
       raise Http404

所以我们在这里做的只是在try中包含视图中的所有代码:except:所以如果在try块中出现问题,它将移动到除了块之外,并且有一个将返回404页面的raise指令。 请在您的模板中创建404.html和500.html,即使有500个

也能获得精美的页面