Django反应渲染
在react with django上创建SPA的最佳方式是什么? 仅使用rest api或Django呈现的不同部分(例如,作为简单模板)。使用django render有什么好处?Django反应渲染,django,reactjs,Django,Reactjs,在react with django上创建SPA的最佳方式是什么? 仅使用rest api或Django呈现的不同部分(例如,作为简单模板)。使用django render有什么好处? 谢谢。我们目前已将React mini应用程序集成到现有的Django项目中 我们只需返回包含道具的上下文: class HelloWorld(DetailView): template_name = '/path/to/your/template.html' def get_context_d
谢谢。我们目前已将React mini应用程序集成到现有的Django项目中 我们只需返回包含道具的上下文:
class HelloWorld(DetailView):
template_name = '/path/to/your/template.html'
def get_context_data(self, **kwargs):
props = {
// your props/data
}
context = {
'component': 'overview.js',
'title': 'Hello World',
'props': json.dumps(props)
}
return context
然后在模板中,我们将道具渲染到全局窗口范围:
<script>
// Make sure to escape your props to prevent XSS! See here for the source
// for the json filter: https://gist.github.com/pirate/c18bfe4fd96008ffa0aef25001a2e88f
window.props = {{props|safe}};
</script>
<script src="/path/to/your/js/{{component}"></script>
您可以在此处阅读有关此方法的更多信息:
是否对所有渲染仅使用一个视图?您是使用react路由器进行路由还是使用django进行路由?我们目前使用django进行路由。但最终将转向使用React路由器,并使用我们的REST api而不是Django templatingI相反地从仅React呈现迁移到Django React,因为我有不同的域、团队等。Clean React不提供所有功能,我想……在阅读了一些主题之后,我选择了以下方案:point all-Django。登录页面,付款页面-使用Django URL发送。但在主应用程序中,使用react路由器执行页面规则。我使用django webpack loader并在主模板中加载我的包(js、css)。然后在应用程序中创建初始数据请求和我的操作请求
ReactDOM.render(React.createElement(OverviewApp, window.props), document.getElementById('root');)