11-4 404和500页面配置


# 全局404页面配置
handler404 = 'users.views.page_not_found'


逻辑功能


def page_not_found(request):
    """
    全局 404 处理函数
    """
    from django.shortcuts import render_to_response
    response = render_to_response('404.html',{})
    response.status_code = 404
    return response

现在访问一个不存在的路径还是会


Page not found (404)
Request Method:	GET
Request URL:	http://127.0.0.1:8000/info
Using the URLconf defined in MxOnline.urls, Django tried these URL patterns, in this order:

^xadmin/
^$ [name='index']
^login/$ [name='login']
^logout/$ [name='logout']
^register/$ [name='register']
^captcha/
^active/(?P<active_code>.*)/$ [name='user_active']
^forget/$ [name='forget_pwd']
^reset/(?P<active_code>.*)/$ [name='reset_pwd']
^modify_pwd/$ [name='modify_pwd']
^org/
^course/
^users/
^media/(?P<path>.*)$
The current URL, info, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

解决办法:当不是debug模式时,django不会自动帮我们找404页面,需要我们自己配置静态文件路径

# DEBUG = True
# 生产环境一定要改为 False 
DEBUG = False

# ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['*']

STATIC_ROOT = os.path.join(BASE_DIR, 'static')



C:\Users\hlg\PycharmProjects\MxOnline\MxOnline\urls.py





重启:效果



500 页面配置


def page_error(request):
    """
    全局500处理函数
    """
    from django.shortcuts import render_to_response
    response = render_to_response('500.html',{})
    response.status_code = 500
    return response



handler500 = 'users.views.page_error'



测试500页面



效果:





























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值