Django 配置404页面

修改settings.py

DEBUG = False  # 开发环境下为True,此时我们改为False
ALLOWED_HOSTS = ['*']  # 访问地址,127.0.0.1,自己的ip,如172.21.21.21(随便写的),...

静态文件配置


STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static').replace('\\', '/')

在settings.py文件目录中,添加文件view.py

# 404
def page_not_found(request, exception):
    return render(request, '404.html')

# 500
def page_error(request):
    return render(request, '500.html')

在settings.py文件目录中,修改文件urls.py

from django.conf.urls import url
from . import view

urlpatterns = [
    ...
]
handler404 = view.page_not_found
handler500 = view.page_error

可能会报错

ERRORS:
?: (urls.E007) The custom handler404 view 'index.views.page_not_found' 
does not take the correct number of arguments (request, exception).

System check identified 1 issue (0 silenced).

说明接口函数少传了一个参数

参数:

https://blog.csdn.net/geek_xiong/article/details/90349476

https://www.cnblogs.com/tynam/archive/2018/12/26/10182462.html

https://blog.csdn.net/u010429424/article/details/77363531

https://blog.csdn.net/liyyzz33/article/details/85259744

猜你喜欢

转载自www.cnblogs.com/sea-stream/p/11503943.html