canadahetian
V2EX  ›  Django

关于 DJango3.0 以上,如何自定义 500,404 等错误页面的问题,求指点.

  •  
  •   canadahetian · Apr 9, 2020 · 2982 views
    This topic created in 2304 days ago, the information mentioned may be changed or developed.

    今天配置 Django3.0 的错误页面,发现远没有以前的版本那么方便,越改越不适应了.

    settings.py

    DEBUG = False

    ALLOWED_HOSTS = ['*']

    urls.py

    handler404 = views.page_not_found

    handler500 = views.server_error

    views.py

    def page_not_found(request, exception):

    data = {}
    
    return render(request, 'blog/errors/page_404.html', data, status=404)
    

    def server_error(request, exception):

    data = {}
    
    return render(request, 'blog/errors/page_500.html', data)
    

    网上找了各种版本的配置,包括看了官方文档就是配置不成功,不知道是哪里有问题 请各位有配置过 Django3.0 版本的朋友们,帮指点一下,谢谢了!在线等.

    1 replies    2020-04-16 16:09:53 +08:00
    kiccham
        1
    kiccham  
       Apr 16, 2020
    上面这些都不需要。直接在 templates 目录下放 404.html,500.html 就好了,从 Django 2.2 开始我都试了没问题
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2563 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 00:55 · PVG 08:55 · LAX 17:55 · JFK 20:55
    ♥ Do have faith in what you're doing.