标题很糟糕 - 抱歉。
所以说我有一个网站说www.myhairynose.com/。我已经配置了我的urls.py,以便每当有人进入www.myhairynose.com时,它会将它们重定向到index.html(它显示reactjs应用程序)。我已经使用react-router设置了reactjs,当你点击索引页面上的一个按钮时,它会转到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来执行此操作?
答案 0 :(得分:0)
您是否正在使用Django进行渲染反应?我假设你有一些像views.LoadReactView
这样的观点。
你需要的是捕捉所有路线。
from django.contrib import admin
from myapp import views
urlpatterns = [
...
url(r'^admin', admin.site.urls),
url(r'^', views.LoadReactView.as_view()),
]
这应该是您最后的模式。你基本上是在说“检查完所有其他路线后,如果没有匹配,请到这里”。
此外,您还可以将主视图定义为404 handler
urlpatterns = [
...
]
handler404 = views.LoadReactView.as_view