不同的404页面取决于Django中的应用程序

时间:2022-02-04 21:11:31

We are developing a project with several applications using Django. It shares the database, but it has several applications targeting different very different users. Roughly, administrators and final users. The UI of each application is very different. I need to create a 404 error page, but seems that I can only create one for all the project. I would like to create different 404 templates and being able to shown them depending on the application (URL) the user is asking for...

我们正在使用Django开发一个包含多个应用程序的项目。它共享数据库,但它有几个应用程序针对不同的非常不同的用户。粗略地说,管理员和最终用户。每个应用程序的UI都非常不同。我需要创建一个404错误页面,但似乎我只能为所有项目创建一个。我想创建不同的404模板,并能够根据用户要求的应用程序(URL)显示它们...

For general, clearly invalid URL it's easy, but in the code there are other ways of launching exceptions, like get_object_or_404 calls.

一般来说,显然无效的URL很容易,但在代码中还有其他启动异常的方法,比如get_object_or_404调用。

Anyone knows a way of doing that?

谁知道这样做的方法?

2 个解决方案

#1


4  

It's not at all true that you can only create a single 404 page for the entire app. The documentation explains how you can create a specific 404 handler view, which of course can look at the value of request.path to see what URL was requested and render the relevant template.

您只能为整个应用程序创建一个404页面,这一点都不是真的。该文档说明了如何创建特定的404处理程序视图,当然可以查看request.path的值以查看请求的URL并呈现相关模板。

#2


4  

Write a 404 view by setting handler404, and not just a template. In that view, try to figure from the url which 404 you should show, and render that.

通过设置handler404而不仅仅是模板来编写404视图。在该视图中,尝试从URL中显示您应该显示的404,然后渲染它。

#1


4  

It's not at all true that you can only create a single 404 page for the entire app. The documentation explains how you can create a specific 404 handler view, which of course can look at the value of request.path to see what URL was requested and render the relevant template.

您只能为整个应用程序创建一个404页面,这一点都不是真的。该文档说明了如何创建特定的404处理程序视图,当然可以查看request.path的值以查看请求的URL并呈现相关模板。

#2


4  

Write a 404 view by setting handler404, and not just a template. In that view, try to figure from the url which 404 you should show, and render that.

通过设置handler404而不仅仅是模板来编写404视图。在该视图中,尝试从URL中显示您应该显示的404,然后渲染它。