我使用 reactjs 作为前端,使用 django 作为后端。React 路由器用于路由。当我刷新由react-router路由的页面时,我得到 django 404 Page Not Found error
。如果我刷新主页,我不会收到任何此类错误,因为主页也是由 django 模板使用其 url 呈现的。
我必须在 webpack 中配置它吗?我的项目结构是我将 django 和 reactjs 分开。我创建了一个文件夹作为 reactjs 文件所在的前端。
更新
主页模板包含诸如 addrestaurant 之类的路线的所有链接。
我的 webpack.config 文件
const path = require("path");
if(!process.env.NODE_ENV) {
process.env.NODE_ENV = 'development';
}
module.exports = {
entry: [
'./src/index.js'
],
output: {
path: path.join("../app/static/build/", "js"),
filename: "app.js",
publicPath: "../app/static/build/"
},
devtoo: 'source-map',
debug: true,
module: {
loaders: [{
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['react', 'es2015', 'stage-1']
}
},
{test: /\.(jpe?g|png|gif|svg)$/i, loader: "url-loader?name=images/[name].[ext]"},
]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
devServer: {
historyApiFallback: true,
contentBase: './'
}
};
网址.py
urlpatterns = [
url(r'^', views.home, name="homePage"),
url(r'^(?:.*)/?$', views.home),
]
主页.html
{% extends 'base.html' %}
{% block title %} Foodie | Homepage {% endblock title%}
{% block content %}
<div class="homepage">
</div>
{% endblock %}
{% block js %}
{{ block.super }}
<script type="text/javascript">
var data = {
isUserAuthenticated:{% if request.user.is_authenticated %}true{% else %}false{% endif %}
};
console.log('data',data);
$(function() {
app.showHomePage(".homepage",data);
});
</script>
{% endblock %}
索引.js
window.app = {
showHomePage: function(id,data){
render(
<Provider store={createStoreWithMiddleware(reducers)}>
<Router>
<App />
</Router>
</Provider>, document.querySelector(id)
);
},
}
Banner 是 App 组件的子组件
const Banner = (props) => (
<div className="navbar-container">
<div className="ui container">
<div className="ui large secondary menu">
<a className="toc item">
<i className="sidebar icon"></i>
</a>
<div className="item logo">
<div className="ui logo shape">
<div className="sides">
<div className="active ui side">
Foodie
</div>
</div>
</div>
</div>
<Link to="/restaurant" className="active item tab">Home</Link>
<Link to='/addrestaurant' className='item tab'>Add Restaurant</Link>
<Link to="/products" className="item tab">Products</Link>
<div className="right item">
<a href="" id="bookingInfoButton" className="ui white inverted button">Booking</a>
</div>
</div>
</div>
</div>
);
export default Banner;