执行自定义Django 404页面的代码 [英] Executing code for a custom Django 404 page

查看:192
本文介绍了执行自定义Django 404页面的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在准备部署我的第一个Django应用程序,并且打了一个路障。我的基本模板依赖于我传递会话对象,以便它可以读出当前登录的用户名。当我控制调用模板的代码时,这不是问题。



但是,作为准备部署此应用程序的一部分,我需要创建一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆