• Django 配置出错模板


    常见的出错模板有:

    404.html : [page not found] 页面未找到……
    500.html : [server error] 内部错误……

    403.html : [HTTP Forbidden] 禁止访问……

    400.html : [bad request] 页面已删除、更名、过期或暂不可用……

     

    修改如下:

    settings.py

    ALLOWED_HOSTS = ['0.0.0.0' , ]
    #debug = True
    

    urls.py

    from django.conf.urls import url , include
    from django.contrib import admin
    import audios
    
    handler404 = 'audios.views.my_custom_page_not_found_view' #HttpResponseNotFound
    handler500 = 'audios.views.my_custom_error_view'
    handler403 = 'audios.views.my_custom_permission_denied_view' # HttpResponseForbidden
    handler400 = 'audios.views.my_custom_bad_request_view'
    
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^audios/', include('audios.urls')),
    ]
    
    $ tree templates/custom/
    templates/custom/
    ├── 400.html
    ├── 403.html
    ├── 404.html
    └── 500.html
    

    audios/views.py

    def my_custom_page_not_found_view(request):
        return render_to_response('custom/404.html')
    
    def my_custom_error_view(request):
        return render_to_response('custom/500.html')
    
    def my_custom_permission_denied_view(request):
        return render_to_response('custom/403.html')
    
    def my_custom_bad_request_view(request):
        return render_to_response('custom/400.html')
    

      

    DONE DONE DONE

     

  • 相关阅读:
    “5W1H”带你来学习JavaScript
    [windows+cocos2dx]文本类
    server后台TCP连接存活问题
    (七):处理MFC
    Servlet中的request对象、重定向和转发的差别(6)
    ORACLE 存储函数
    【@伏草惟存@】7年系列博文精选
    笔记14:Docker 部署Python项目
    笔记13:Python 和 Elasticsearch 构建简易搜索
    笔试12:Bootstrap知识
  • 原文地址:https://www.cnblogs.com/zsr0401/p/6514927.html
Copyright © 2020-2023  润新知