# 全局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页面
效果: