我如何配置 django url 以使其在显示 404 时重新路由到 Reactjs

How do i configure django urls to make it so if it displays 404, it reroutes to Reactjs

标题太糟糕了 - 对此感到抱歉。

所以说我有一个网站说 www.myhairynose.com/。我已经配置了我的 urls.py,这样每当有人输入 www.myhairynose.com 时,它就会将他们重定向到 index.html(显示 reactjs 应用程序的地方)。我已经将 reactjs 设置为 react-router 并设置为当您单击索引页面上的按钮时它会转到 www.myhairynose.com/#/webpage.

事情是...我不想要那个。

我希望它转到 www.myhairnose。com/webpage。但是,我想做出反应来处理这个问题,而不是 django。所有这些 url 都应该在 index.html 的同一个应用程序中。我如何配置 django urls.py 以便如果我输入 www.myhairynose.com/webpage 它会转到 www.myhairnose.com ReactJs 应用程序的索引,然后检查路由是否有网页。否则说如果我去了 www.myhairynose.com/asdkjaskldsaj (反应路线没有)它应该显示 404.

______TLDR______ 基本上我想要发生的事情:

User goes to website.com/name
>if website.com/name exists in django - it returns a html doc
>if not then it redirects to website.com/ where ReactJs will handle the url and see if it matches any of the routers
...> if it does, then it displays that route
...> if not then it displays a django 404 or a custom 404 page.

如何配置 django 和 reactjs 来执行此操作?

您是否使用 Django 来渲染 React?我假设您有一些像 views.LoadReactView.

这样的观点

你需要的是一条包罗万象的路线。

from django.contrib import admin
from myapp import views

urlpatterns = [
    ...
    url(r'^admin', admin.site.urls),
    url(r'^', views.LoadReactView.as_view()),
] 

这应该是您的最后一个模式。您实际上是在说 "after checking all the other routes, if there are no matches, go here".

此外,您实际上也可以将主视图定义为 404 handler

urlpatterns = [
    ...
]

handler404 = views.LoadReactView.as_view