django-cms的自定义错误页面

时间:2022-04-29 23:19:22

Supposedly a trivial task to server 403/404/500 error pages when using django-cms. Followed instructions on an old forum post to create this:

据说使用django-cms时服务器403/404/500错误页面是一项微不足道的任务。按照旧论坛帖子上的说明创建:

from cms.views import details

def custom_404(request):
    response = details(request, 'page-not-found')
    response.status_code = 404
    return response
...

Urls.py has some lines like this:

Urls.py有一些像这样的行:

handler404 = 'error_pages.views.custom_404'
...

From traceback django cms can't locate 404 page:

来自traceback django cms找不到404页面:

File "/home/username/.virtualenvs/venv/lib/python2.7/site-packages/cms/views.py", line 22, in _handle_no_page
    raise Http404('CMS: Page not found for "%s"' % slug)

Http404: CMS: Page not found for "page-not-found"

Obviously added the required custom pages in django-cms with the slug: 'page-not-found'. Am I missing something obvious? Running on production server with debug=False. Running django-cms 2.4.2 (edit)

很显然,在django-cms中添加了所需的自定义页面:slug:'page-not-found'。我错过了一些明显的东西吗使用debug = False在生产服务器上运行。运行django-cms 2.4.2(编辑)

Perhaps it is better to just serve plain ol' error messages with hardcoded stylesheets?

也许用硬编码的样式表来提供简单的错误消息会更好吗?

3 个解决方案

#1


9  

After walking into countless walls over-thinking the issues, I just went with using the basic 403/404/500 handlers:

走进无数的墙壁过度思考问题后,我只是使用了基本的403/404/500处理程序:

from django.utils.functional import curry
from django.views.defaults import *
handler500 = curry(server_error, template_name='500.html')
handler404 = curry(page_not_found, template_name='404.html')
handler403 = curry(permission_denied, template_name='403.html')

Created the templates for each error and put in absolute URLs for the stylesheets.

为每个错误创建模板,并为样式表添加绝对URL。

Problem solved. Wasted a bunch of time on something this trivial.

问题解决了。在这件微不足道的事情上浪费了大量时间。

#2


3  

Here is a working (with DEBUG at True or False) 404 handler:

这是一个工作(使用DEBUG在True或False)404处理程序:

def handler404(request):
    if hasattr(request, '_current_page_cache'):
        delattr(request, '_current_page_cache')

    response = details(request, '404')
    response.status_code = 404
    return response

#3


0  

EDIT / Easy solution

编辑/简易解决方案

After more searching and thinking, an easier solution would be to create the default/standard 404.html, and therein use django-cms static placeholders...as easy as it gets!

经过更多的搜索和思考,一个更简单的解决方案是创建默认/标准404.html,并在其中使用django-cms静态占位符......就像它获得的一样简单!

Original (still working) Answer

原始(仍在工作)答案

After struggling updating my handler404 from an old cms project, and not finding any infos on this topic, and the accepted answer not being a real solution to the problem, I investigated and found a version that works in django-cms 3.4.

在努力从旧的cms项目更新我的handler404,并没有找到关于这个主题的任何信息,并且接受的答案不是问题的真正解决方案之后,我调查并发现了一个适用于django-cms 3.4的版本。

Worth noting

值得注意

  • delete the _current_page_cache on the request
  • 删除请求中的_current_page_cache
  • set request.current_page, or cms_tags will not use your 404 page and render empty
  • 设置request.current_page,或者cms_tags不会使用您的404页面并呈现为空
  • call the main cms details view for rendering the page
  • 调用主cms详细信息视图以呈现页面
  • finally, call response.render() (as mentioned in comments)
  • 最后,调用response.render()(如评论中所述)

The view

风景

def handler404(request):
    if hasattr(request, '_current_page_cache'):  # we'll hit the cache otherwise
        delattr(request, '_current_page_cache')
    page = get_page_from_request(request, '404')
    request.current_page = page  # templatags seem to use this.
    response = details(request, '404')  # the main cms view
    if hasattr(response, 'render'):  # 301/302 dont have it!
        response.render()  # didnt know about this, but it's needed
    response.status_code = 404  # the obvious
    return response

#1


9  

After walking into countless walls over-thinking the issues, I just went with using the basic 403/404/500 handlers:

走进无数的墙壁过度思考问题后,我只是使用了基本的403/404/500处理程序:

from django.utils.functional import curry
from django.views.defaults import *
handler500 = curry(server_error, template_name='500.html')
handler404 = curry(page_not_found, template_name='404.html')
handler403 = curry(permission_denied, template_name='403.html')

Created the templates for each error and put in absolute URLs for the stylesheets.

为每个错误创建模板,并为样式表添加绝对URL。

Problem solved. Wasted a bunch of time on something this trivial.

问题解决了。在这件微不足道的事情上浪费了大量时间。

#2


3  

Here is a working (with DEBUG at True or False) 404 handler:

这是一个工作(使用DEBUG在True或False)404处理程序:

def handler404(request):
    if hasattr(request, '_current_page_cache'):
        delattr(request, '_current_page_cache')

    response = details(request, '404')
    response.status_code = 404
    return response

#3


0  

EDIT / Easy solution

编辑/简易解决方案

After more searching and thinking, an easier solution would be to create the default/standard 404.html, and therein use django-cms static placeholders...as easy as it gets!

经过更多的搜索和思考,一个更简单的解决方案是创建默认/标准404.html,并在其中使用django-cms静态占位符......就像它获得的一样简单!

Original (still working) Answer

原始(仍在工作)答案

After struggling updating my handler404 from an old cms project, and not finding any infos on this topic, and the accepted answer not being a real solution to the problem, I investigated and found a version that works in django-cms 3.4.

在努力从旧的cms项目更新我的handler404,并没有找到关于这个主题的任何信息,并且接受的答案不是问题的真正解决方案之后,我调查并发现了一个适用于django-cms 3.4的版本。

Worth noting

值得注意

  • delete the _current_page_cache on the request
  • 删除请求中的_current_page_cache
  • set request.current_page, or cms_tags will not use your 404 page and render empty
  • 设置request.current_page,或者cms_tags不会使用您的404页面并呈现为空
  • call the main cms details view for rendering the page
  • 调用主cms详细信息视图以呈现页面
  • finally, call response.render() (as mentioned in comments)
  • 最后,调用response.render()(如评论中所述)

The view

风景

def handler404(request):
    if hasattr(request, '_current_page_cache'):  # we'll hit the cache otherwise
        delattr(request, '_current_page_cache')
    page = get_page_from_request(request, '404')
    request.current_page = page  # templatags seem to use this.
    response = details(request, '404')  # the main cms view
    if hasattr(response, 'render'):  # 301/302 dont have it!
        response.render()  # didnt know about this, but it's needed
    response.status_code = 404  # the obvious
    return response