执行自定义Django 404页面的代码 [英] Executing code for a custom Django 404 page
问题描述
但是,作为准备部署此应用程序的一部分,我需要创建一个404.html页面。我扩展了我的基本模板就像我已经完成了其他页面,但我没有看到一种方法传递会话对象,以便我可以利用它。有没有办法让Django调用自定义方法来渲染你的404,而不仅仅是渲染你的404.html?
您需要覆盖404错误的默认视图处理程序。以下是关于如何创建自己的自定义404查看功能的文档:
http://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views
I am getting ready to deploy my first Django application and am hitting a bit of a roadblock. My base template relies on me passing in the session object so that it can read out the currently logged in user's name. This isn't a problem when I control the code that is calling a template.
However, as part of getting this app ready to be deployed, I need to create a 404.html page. I extended my base template just like I've done with my other pages, but I don't see a way to pass in the session object so that I can utilize it. Is there a way to have Django call a custom method to render your 404 rather than just rendering your 404.html for you?
You need to override the default view handler for the 404 error. Here is the documentation on how to create your own custom 404 view function:
http://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views
这篇关于执行自定义Django 404页面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!