django 自定义404页面

在django中使用我们自定义的404页面,需要四个步骤:

  • 1、在templates中添加404.html
  • 2、修改settings.py
  • 3、修改views.py
  • 4、修改urls.py

1、在templates中添加404.html

在templates目录里,添加一个404.html文件,我们自定义的404错误页面就写在这个文件里。比如我的html是这么写的:

404.html

2、修改views.py

在views.py中,增加一个方法返回自定义页面

def page_not_found(request):
    return render_to_response('404.html')

3、修改urls.py

主目录里的urls.py

handler404 = app1.views.page_not_found # 固定变量名,复制响应模板的函数
# 同样可以设置
handler404 = page_not_found
handler500 = page_error

  

猜你喜欢

转载自www.cnblogs.com/wwg945/p/9147090.html
今日推荐