我正在尝试在项目中解耦我的前端和后端。我的前端由以下组成reactjs
和路由将完成react-router
,我的后端如果做成形式Django
我计划使用前端对 Django 进行 API (ajax) 调用。
现在我不确定如何让这两端正确地相互通信。
这里是link https://github.com/liondancer/django-cherngloong到我的项目
这是我的项目结构:
/cherngloong
/app (frontend)
/cherngloong
/templates
index.jtml
urls.py
settings.py
...
/contact
urls.py
views.py
I use webpack
构建我所有的 JS 和 CSS 并将其放入index.html
with webpack_loader
看起来像这样:
{% load render_bundle from webpack_loader %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>
<body>
<div id="app"></div>
{% render_bundle 'main' %}
</body>
</html>
In Django
这是我的cherngloong/urls.py
:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', TemplateView.as_view(template_name='index.html')),
url(r'^api/', include('contact.urls'))
]
urlpatterns += staticfiles_urlpatterns()
我不想从 django 提供我的应用程序或让 django 在任何 url 上提供相同的视图。
这是我的react-router
routes:
var routes = (
<Router>
<Route path="/" component={ Views.Layout } >
<Route path="contact" component={ Views.Contact }/>
</Route>
<Route path="*" component={ Views.RouteNotFound } />
</Router>
);
export default routes;
我当前可以运行服务器,但是当我运行前端部分时,我在开发人员工具中看到了这一点
http://localhost:8000/static/public/js/main.js Failed to load resource: the server responded with a status of 404 (NOT FOUND)