【问题标题】:Im using Django as Backend and Reactjs as Frontend, So what must i use to route the urls我使用 Django 作为后端和 Reactjs 作为前端,那么我必须使用什么来路由 url
【发布时间】:2019-10-13 12:11:35
【问题描述】:

我必须使用什么来路由 url? 使用 Django 中的 React-Router 或 Urls?

ReactJS 路由:

<Router>
<Route path="/" exact component={StatefullComponent} />
<Route path="/blog" exact component={BlogPost} />
<Route path="/blog/detail/:id" component={DetailPost} />
</Router>

Django 路线(网址):

urlpatterns = [
    url(r'^blog/', include('blog.urls', namespace='blog')),
]

问题: 1.哪条路线更好? 2.我的网址可以同时使用两条路线吗? 3.如果我使用django urls,我的前端会像react那样使用单页路由吗?

【问题讨论】:

  • 1.取决于您项目的需要,2. 是的,3. 是的
  • 感谢您的回复,如果我想同时使用,我该如何组合呢?
  • 最好使用 React 路由器来确定加载哪个页面,因为它更快,但您还需要使用 Django 的 URL 来为您的 React 应用程序提供服务。两者都是必需的。假设你想在前面查看博客详细信息并处理博客详细信息,你怎么能告诉 Django 你要加载哪个页面详细信息?所以你还需要使用 Django 路由器。

标签: django reactjs django-rest-framework react-router django-urls


【解决方案1】:

我个人更喜欢 ReactJS 处理路由,使用 django 来提供响应。所以我更喜欢在 django 中使用以下模式:

urlpatterns = [
    url(r'^.*$', TemplateView.as_view(template_name='react_template_name.html')),
]

在这种模式中,url 接受任何路径和服务器 ReactJs 应用程序。然后React Router 接管并完成其余的路由。

【讨论】:

    猜你喜欢
    • 2022-06-19
    • 1970-01-01
    • 2019-01-05
    • 2014-10-21
    • 1970-01-01
    • 2019-09-29
    • 2021-12-24
    • 2015-03-09
    • 1970-01-01
    相关资源
    最近更新 更多