[英]How can I get django to redirect all unknown urls to `/`
How can I get django to redirect all unknown urls to /
我怎样才能让 django 将所有未知的 url 重定向到/
Since the react part of the project uses react-router, it changes the url in the SPA.由于项目的react部分使用了react-router,它改变了SPA中的url。 So if I go to /options
in the app, it works, but if I were to enter the url and cold load from http://example.com/options
, uwsgi returns a 404.因此,如果我转到应用程序中的/options
,它可以工作,但是如果我要从http://example.com/options
输入 url 和冷加载,则 uwsgi 会返回 404。
What is the proper thing to do to prevent this?为了防止这种情况发生,正确的做法是什么?
Regarding your second question,use 404 handler
关于你的第二个问题,使用404 handler
# root_dir/views.py
from django.http.response import HttpResponseRedirect
def handler404(request, *args, **kwargs):
return HttpResponseRedirect('/')
# root_dir/urls.py
urlpatterns = [
path('admin/', admin.site.urls),
...,
...,
]
handler404 = 'root_dir.views.handler404'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.