是否可以在不改变项目级别的任何内容的情况下为Django应用程序包含自定义404视图?

时间:2021-09-07 18:14:49

I'm creating a Django app and trying to have it touch the containing project in as few places as possible.

我正在创建一个Django应用程序并尝试让它在尽可能少的地方触摸包含项目。

I've created a custom 404 view which works as expected but only when I add handler404 to the project-level urls.py.

我创建了一个自定义404视图,它按预期工作,但只有当我将handler404添加到项目级urls.py时。

I want the custom 404 view that I've written to apply to this particular app only, but from the information I've come across it appears that this may not be possible. Adding handler404 to the app-level urls.py does not have any effect.

我希望我编写的自定义404视图仅适用于此特定应用,但从我遇到的信息看来,这可能是不可能的。将handler404添加到app-level urls.py没有任何效果。

Does Django support custom 404 views at the application level?

Django是否支持应用程序级别的自定义404视图?

1 个解决方案

#1


0  

Instead of finishing your view with:

而不是完成你的观点:

return render_to_response(...)

Get the response object and check its status:

获取响应对象并检查其状态:

out = render_to_response(...)

if out.status_code==404:
    # redirect to your 404 page
else:
    return out

#1


0  

Instead of finishing your view with:

而不是完成你的观点:

return render_to_response(...)

Get the response object and check its status:

获取响应对象并检查其状态:

out = render_to_response(...)

if out.status_code==404:
    # redirect to your 404 page
else:
    return out