我使用Django作为后端,使用Reactjs作为前端,所以我必须使用什么来路由网址

时间:2019-05-28 02:02:58

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

我必须使用什么路由网址? 使用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 Route(Urls):

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

问题: 1.哪个路线更好? 2.我可以同时使用两个路由作为网址吗? 3.如果我使用django网址,我的前端会像反应一样使用单页路由吗?

1 个答案:

答案 0 :(得分:0)

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

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

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