首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >重构Django应用程序-将monolith拆分为REST和前端

重构Django应用程序-将monolith拆分为REST和前端
EN

Software Engineering用户
提问于 2018-02-28 05:52:00
回答 1查看 1.2K关注 0票数 0

我正在重构一个Django网络应用程序。它是用通常的MVT风格编写的,我想将其更改为REST +前端方法。在我的第一次迭代中,我想做尽可能少的更改。我的计划是:

  • 将后端拆分为REST和一个面向Django的简单客户端应用程序(新的“前端”)
  • REST中的业务逻辑
  • Django“前端”将与REST对话,以执行操作和收集数据。
  • REST与Django REST Framework (DRF)一起使用,保留当前的模型,并且对视图几乎不做任何更改。
  • 为前端保留尽可能多的Django模板。也就是说,我不想为前端引入任何新技术(没有Javascript SPA框架ala AngularJs、EmberJs或React)。

以后,重构的迭代可能会转移到由DRF应用程序支持的React。

有可能让Django“前端”与REST对话吗?

EN

回答 1

Software Engineering用户

发布于 2018-05-08 14:41:26

有可能让Django“前端”与REST对话吗?

首先,将数据移动到无头CMS。然后,向settings.py中的无头CMS添加一个API令牌:

代码语言:javascript
运行
复制
/webapp/settings.py
# Storyblok configuration for your own space
STORYBLOK_CONFIGURATION = {
  'PRIVATE_TOKEN': 'YOUR_PREVIEW_TOKEN',
  'HOME_SLUG': 'home'
}

然后构造Django视图层,以便每个模板映射到一个API端点。将独立服务器上的静态资产和对其的代理请求分开。例如:

代码语言:javascript
运行
复制
/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是这样的:

代码语言:javascript
运行
复制
<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>

参考资料

票数 1
EN
页面原文内容由Software Engineering提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://softwareengineering.stackexchange.com/questions/366712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档