javascript - Django - AngularJs 项目结构

标签 javascript python angularjs django angular

经过几天的研究后,我还没有决定使用 Django 的 AngularJs 的最佳结构是什么。GitHub 上有许多项目可用,网上的教程也使用 Django 项目静态文件夹中的 Angular 文件。下面的结构主要遵循带有 Django 的 Angular 1。

  Project Name
     app
     project name
     templates
     static
        angular_app
            app1
               app1.component.js
               app1.module.js
             app.js
         angular_templates (static templates for rendering in routes)
     manage.py    

在 Angular 2 中,我们必须使用节点服务器,据我所知,大多数经验丰富的 Angular 开发人员也使用其他后端技术,他们使用这种结构

  AngularJs Project
      app1
         app1.component.js
         app1.module.js
       angular_templates (static templates for rendering in routes)
       app.js

  Django Project ( or any other backend project)
     app
     project name
     templates
     static
     manage.py

这种项目结构的优点: 我们可以通过替换后端项目文件夹来使用任何其他后端技术。 只有API依赖于后端项目。前端完全独立于后端。

So, the question is what's the best structure for the Django-Angular project? if the second option is good, we have to deploy our angular and Django code separately on the same domain? If yes how to do it ?

最佳答案

如果您愿意,可以将两者结合起来。我用 Rails 后端做了类似的事情,它对我很有帮助。 (请注意,我不熟悉 Django,但我假设同样的逻辑适用。)

假设 Django 项目中的静态文件夹将其中的任何内容作为静态内容提供,我不建议将 Angular 项目包含在该文件夹中。相反,让您的 Angular 文件夹成为静态文件夹的同级文件夹。但是,配置您的 Angular 项目以将其最终输出构建到 Django 静态文件夹中。

所以,你的项目看起来像:

 Django Project ( or any other backend project)
     app
     project name
     templates
     static  (receives minified output from AngularJS project)
     manage.py
     AngularJs Project
        app1
           app1.component.js
           app1.module.js
        angular_templates (static templates for rendering in routes)
        app.js

这样做有几个好处: 1. 项目或多或少还是分开的——当你做 Angular 工作时,你在 AngularJS 文件夹中工作,否则你在其他文件夹中工作以进行 Django 工作

  1. 部署更简单 - 您构建 AngularJS 工件(js、css、index.html)并 checkin 它们。然后部署 Django 文件夹即可部署所有内容

  2. 您仍然可以从 AngularJS 生态系统的工具中受益,因为就所有意图而言,它仍然在自己的文件夹中并且是隔离的。

  3. 一切都在一个存储库中,这可以使源代码管理变得更加容易。

有几个缺点 - 你正在检查 AngularJS 构建工件并且你的 AngularJS 更改与你的服务器端更改混合在一起,但我发现除非服务器端和客户端团队完全独立,这还没有被证明是一个问题。

关于javascript - Django - AngularJs 项目结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42724352/

相关文章:

python - 通过python执行带空格的sudo命令

python - SecureAuth 原因 - ShareplumRequestError : Shareplum HTTP Post Failed : 403 Client Error: Forbidden for url

angularjs - 无法从数据分配类型,因此默认为 Angular ui-grid 中的字符串

angularjs - Sublime Text 2 : Different language highlighting based on context? (a la Webstorm)

javascript - 如何使用页面 JavaScript 检查是否安装了 Firefox WebExtension?

javascript - 如何更改 react 中复选框项目的值? (更改sql中的一个值)

javascript - 阻止 javascript 原型(prototype)设计杀死 for 循环

python - 子类化 multiprocessing.managers.BaseProxy

javascript - 从另一个范围遍历范围内的对象?

javascript - 使用 javascript/lodash 从输入字符串中解析 html 内容