我正在重构一个Django网络应用程序。它是用通常的MVT风格编写的,我想将其更改为REST +前端方法。在我的第一次迭代中,我想做尽可能少的更改。我的计划是:
以后,重构的迭代可能会转移到由DRF应用程序支持的React。
有可能让Django“前端”与REST对话吗?
发布于 2018-05-08 14:41:26
有可能让Django“前端”与REST对话吗?
首先,将数据移动到无头CMS。然后,向settings.py中的无头CMS添加一个API令牌:
/webapp/settings.py
# Storyblok configuration for your own space
STORYBLOK_CONFIGURATION = {
'PRIVATE_TOKEN': 'YOUR_PREVIEW_TOKEN',
'HOME_SLUG': 'home'
}
然后构造Django视图层,以便每个模板映射到一个API端点。将独立服务器上的静态资产和对其的代理请求分开。例如:
/webapp/views/ All your layouts and components at one space - if you add a new or change an existing Jinja2 component (.html.j2) the gulp build will trigger an instant reload for you in the browser - also each component is a representation of a storyblok component. If you create a headline component in storyblok - make sure to create a headline.html.j2 as well - so the django application knows which component to render.
Storyblok中组件的计数器部分就在您已经下载的python项目中。您可以在/webapp/view/components/文件夹中找到teaser.html.j2。我们使用jinja2作为模板-当然,您可以用任何其他模板引擎来更改它。对于这个示例,teaser.html.j2是这样的:
<div class="teaser">
<!--
The _editable attribute makes the next
DOM-element clickable so the sidebar can
show the right component.
-->
{{ blok._editable|safe }}
<div class="teaser__inner">
<h1>
<!--
You can access every attribute you
define in the schema in the blok variable
-->
{{ blok.headline }}
</h1>
<h2>
You can create new components like this, to create your own set of components.
</h2>
</div>
</div>
https://softwareengineering.stackexchange.com/questions/366712
复制相似问题