第三百九十一节,Django+Xadmin打造上线标准的在线教育平台—404,403,500页面配置

第三百九十一节,Django+Xadmin打造上线标准的在线教育平台—404,403,500页面配置

 

路由映射
在全局也就是根目录里的urls.py里配置404路由映射

注意:不是写在urlpatterns列表里,而是独立写

handler404 = '404逻辑函数'
函数需要从app路径开始

# 全局404页面配置
handler404 = 'app_users.views.pzhi404'

 

逻辑函数

可以写在任意一个app的views.py文件里

render_to_response('设置要显示的404页面', {})

def pzhi404(request):
    from django.shortcuts import render_to_response
    response = render_to_response('404.html', {})
    response.status_code = 404  # 状态码404
    return response

 

404html里静态文件配置

<!DOCTYPE html>
<html>
{% load staticfiles %} {# 启用静态文件引用#}
<head>
    <meta charset="UTF-8">
    <title>404</title>
    <link rel="stylesheet" type="text/css" href="{% static 'css/reset.css'%}">
    <link rel="stylesheet" type="text/css" href="{% static 'css/animate.css'%}">
    <link rel="stylesheet" type="text/css" href="{% static 'css/style.css'%}">
    <script type="text/javascript" src="{% static 'js/jquery.min.js'%}"></script>
</head>
<body class="bg404 errorpage">
<section>
    <div class="wp">
        <div class="cont">
            <img src="{% static 'images/pic404.png'%}"/>
            <br/><br/><br/><br/>
            <p>wow~这个页面被外星人抢走了~</p>
            <br/>
            <span>Wow~ this page was the alien took ~</span>
        </div>
    </div>
</section>
</body>
</html>

 

 

配置settings.py文件

注意:如果要设置404页面显示,就必须在settings.py文件关闭调试模式

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False                                # 将调试模式关闭,关闭后访问页面出错不会显示错误(大黄页)
ALLOWED_HOSTS = ['*']                        # 当关闭调试模式时,必须设置允许访问的ip,*代表所有以访问

 

 

当关闭了调试模式时,就必须给静态文件做url路由映射

settings.py

STATIC_URL = '/static/'    # 设置静态文件前缀名称
# 配置静态文件目录
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),   # 设置静态文件路径
]
# 配置静态文件访问路径
STATIC_ROOT = os.path.join(BASE_DIR, 'static')    # 设置上传文件路径

urls.py

from django.conf.urls import url, include                   # 导入django自在的include逻辑
from django.contrib import admin
from django.views.generic import TemplateView               # 导入django自带的TemplateView逻辑
from django.views.static import serve                       # 导入django自带的serve静态资源逻辑
import xadmin                                               # 导入xadmin

from app_users.views import deng_lu, zhu_ce, active_code, logout                       # 导入登录逻辑处理类
from app_organization.views import org_list, course_comment,usercenter_info,touxiang   # 导入授课机构逻辑
from MxOnline.settings import MEDIA_ROOT, STATIC_ROOT


urlpatterns = [
    url(r'^xadmin/', xadmin.site.urls),

    url(r'^index.html', TemplateView.as_view(template_name='index.html'), name='index'),

    # 注册
    url(r'^register.html', zhu_ce.as_view(), name='register'),
    url(r'^captcha/', include('captcha.urls'), name='captcha'),
    url(r'^active/(?P<active_de>.*)/$', active_code.as_view(), name="user_active"),

    # 登录
    url(r'^login.html', TemplateView.as_view(template_name='login.html'), name='login'),
    url(r'^deng_lu', deng_lu.as_view(), name='deng_lu'),
    url(r'^logout', logout.as_view(), name='deng_lu'),

    # 授课机构
    url(r'^org_list.html', org_list.as_view(), name='org_list'),

    # 专门处理静态资源请求映射,也就是media上传文件夹里的请求映射
    url(r'media/(?P<path>.*)$', serve, {'document_root': MEDIA_ROOT}),
    
    # 配置静态文件访问
    url(r'static/(?P<path>.*)$', serve, {'document_root': STATIC_ROOT}),

    url(r'course_comment.html', course_comment.as_view(), name='course_comment'),
    url(r'usercenter_info.html', usercenter_info.as_view(), name='usercenter_info'),

    # 上传头像
    url(r'touxiang/', touxiang.as_view(), name='touxiang')
]


# 全局404页面配置
handler404 = 'app_users.views.pzhi404'

 

 

其他如500页面配置同理

url映射

# 全局404页面配置
handler404 = 'app_users.views.pzhi404'
# 全局500页面配置
handler500 = 'app_users.views.pzhi500'

逻辑

def pzhi404(request):
    from django.shortcuts import render_to_response
    response = render_to_response('404.html', {})
    response.status_code = 404
    return response


def pzhi500(request):
    from django.shortcuts import render_to_response
    response = render_to_response('500.html', {})
    response.status_code = 500
    return response

 

效果

 

posted @ 2017-09-22 10:58  林贵秀  阅读(873)  评论(0编辑  收藏  举报