Django自定义404页面
项目地址:https://github.com/ylpxzx/lifeblog
步骤
- DEBUG =True 改为 DEBUG =False
- ALLOWED_HOSTS = [] 改为 ALLOWED_HOSTS = [‘*’],
- 配置静态路径和静态template,
- 在app下编写404错误处理视图
def page_not_found(request,exception):
return render(request, '404.html')
- 在根urls配置404处理handler
from app.views import page_not_found # 加入该行
urlpatterns = [
path('admin/', admin.site.urls),
url(r'', include('ckeditor_uploader.urls')),
url(r'',include('post.urls')),
url(r'^search/', include(haystack.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
handler404 = page_not_found # 加入该行
- 开启nginx和gunicorn的情况下,需要关闭nginx并杀死gunicorn,重新启动nginx和gunicorn