【发布时间】:2021-04-15 04:32:30
【问题描述】:
您好,我使用 Django 作为后端,使用 React 作为前端。当我刷新页面并且我不在根页面'/' 上时出现问题,页面变为空白。此外,如果我想立即去其他地方,那么主页,如/contact,它也是空白的。我知道这是因为没有像here 所说的那样联系后端服务器。我试图用useHistory 修复它,但它没有显示useContext 错误。
使用 Django - React 堆栈解决此问题的最佳方法是什么?
主 urls.py:
urlpatterns = [
...
path('', TemplateView.as_view(template_name='index.html'))
]
应用 urls.py:
router = routers.DefaultRouter()
router.register('frontpage', FrontpageViewSet, 'frontpage')
router.register('gallery', GalleryViewSet, 'gallery')
urlpatterns = router.urls
反应 index.js:
import {BrowserRouter} from "react-router-dom";
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<App/>
</BrowserRouter>
</React.StrictMode>,
document.getElementById('root')
);
App.js:
const location = useLocation();
return (
<div className="App">
<GlobalStyle/>
<Nav/>
<Switch location={location} key={location.pathname}>
<Route exact path='/'>
<HomePage/>
</Route>
<Route exact path='/gallery'>
<GalleryPage/>
</Route>
<Route exact path='/contact'>
<ContactPage/>
</Route>
</Switch>
</div>
);
【问题讨论】:
标签: python reactjs django url routes