1.在setting.py中,将DEBUG改为False,否则不会生效
ALLOWED_HOSTS = ['*']
DEBUG = False # 生产环境一定要改为False
2.DEBUG为False后,会导致静态资源的路径(STATIC_URL)失效,
简单的做法,在开发时:
启动时使用python manage.py runserver --insecure,以不安全模式运行即可加载静态文件
复杂的方法(django2.2.7,会导致404页面的静态资源的图片无法加载)
setting.py
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
# STATIC_ROOT = os.path.join(BASE_DIR, 'static') 会引起ERROR
urls.py
from EduOnline.settings import STATIC_ROOT
...
re_path('static/(?P.+)', serve,{'document_root':STATIC_ROOT}),
...
handler404 = "appname.views.page_not_found"
3.定义返回404页面的函数
from django.shortcuts import render,render_to_response
def page_not_found(request,exception=404): # 2.2 版本增加了一个参数exception
return render(request, '404.html')
#低版本写法
def page_not_found(request):
response = render_to_response('404.html',{})
response.status_code = 404
return response