简体   繁体   English

我可以在Django项目中使用React Router吗?

[英]Can I use react router in django project?

I have a project that uses React in frontend and Django as backend. 我有一个在前端使用React并在Django后端使用的项目。

Also I use react router in my project and the code looks like this: 我在项目中也使用了React Router,代码如下所示:

<BrowserRouter>
    <Switch>
        <Route path="/" exact component={Home} />
        <Route path="/teachers" exact component={Teachers} />
        <Route path="/courses" exact component={Courses}/>
        <Route path="/about" exact component={About} />
        <Route path="/posts" exact component={Posts} />
    </Switch>
</BrowserRouter>

In django my urls file looks like this: 在Django中,我的url文件如下所示:

urlpatterns = [
    path('', views.index, name="index"),
]

The problem is that whenever I try to navigate to the page from react router I get 404 error from django. 问题是,每当我尝试从React Router导航到页面时,我都会从Django中收到404错误。

Dude I am having the same problem. 杜德,我有同样的问题。 From I have researched the answer is actually to throw a catch all at the end of your urls in django. 根据我的研究,答案实际上是在django中将所有URL置于末尾。 I can only find the old way of doing it. 我只能找到做旧的方法。

url(r'^(%s)?$' % '|'.join(routes), TemplateView.as_view(template_name='index.html'))

I am not sure how to do it with path but I had found it earlier. 我不确定如何使用path进行操作,但我早已找到它。 It might be 有可能

re_path(r'.*', views.index)

Just let me know if that works so I can go home and use it :) 请让我知道是否可行,以便我回家使用它:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM