简体   繁体   English

Django-AngularJs项目结构

[英]Django - AngularJs Project Structure

After researching for the days I'm yet to decide what's the best structure for the AngularJs with Django.Many projects available on GitHub and tutorials on the net using angular files in Django project's static folder.This below structure is mostly followed in Angular 1 with Django. 在研究了几天之后,我还没有决定使用Django的AngularJ的最佳结构是什么.GitHub上有许多项目以及使用Django项目的静态文件夹中的角度文件在网上提供了教程.Angular 1中主要遵循以下结构Django的。

  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    

In Angular 2 we have to use node server and mostly as far I saw with experienced angular developers who also worked with other backend technologies, they use this structure 在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

Advantage of this project structure: We can use any other backend technology by replacing our backend project folder. 该项目结构的优势:通过替换后端项目文件夹,我们可以使用任何其他后端技术。 Only API is dependent on backend project.The frontend is completely independent of the backend. 只有API依赖于后端项目。前端完全独立于后端。

So, the question is what's the best structure for the Django-Angular project? 因此,问题是Django-Angular项目的最佳结构是什么? if the second option is good, we have to deploy our angular and Django code separately on the same domain? 如果第二个选项很好,我们是否必须在同一个域中分别部署angular和Django代码? If yes how to do it ? 如果是,该怎么办?

There is a means of combining the two if you wish. 如果需要,可以将两者结合起来。 I do something similar with a Rails back end and it serves me well. 我对Rails后端做了类似的事情,它对我很有用。 (Note that i'm not familiar with Django, but i assume the same logic applies.) (请注意,我对Django不熟悉,但我认为适用相同的逻辑。)

Assuming that the static folder in your Django project will serve anything in there as static content, i wouldn't recommend including your Angular project in that folder. 假设Django项目中的static文件夹将在其中提供任何内容作为静态内容,我不建议在该文件夹中包含Angular项目。 Instead, let your Angular folder be a sibling folder to the static folder. 相反,让您的Angular文件夹成为静态文件夹的同级文件夹。 However, configure your Angular project to build its final output into the Django static folder. 但是,配置Angular项目以将其最终输出构建到Django静态文件夹中。

So, your project looks like: 因此,您的项目如下所示:

 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

Doing this has a few benefits: 1. The projects are still more or less separate - when doing Angular work, you work in the AngularJS folder, otherwise you work in the other folders for Django work 这样做有一些好处:1.项目或多或少是分开的-在进行Angular工作时,您在AngularJS文件夹中工作,否则在其他文件夹中进行Django工作

  1. Deployment is simpler - you build your AngularJS artifacts (js, css, index.html) and check them in. Then deploying your Django folder deploys everything 部署更加简单-构建AngularJS构件(js,css,index.html)并检入它们。然后部署Django文件夹将部署所有内容

  2. you can still benefit from the tools of the AngularJS eco system because for all intents, it's still in its own folder and isolated. 您仍然可以从AngularJS生态系统的工具中受益,因为就所有意图而言,它仍位于其自己的文件夹中且相互隔离。

  3. Everything is in one repo, which can make source management much easier. 一切都在一个仓库中,这可以使源管理更加容易。

There are a couple of downsides - that you're checking in AngularJS build artifacts and that your AngularJS changes get mixed with your server side changes, but i've found that unless the server side and client side teams are completely independent, it hasn't proven to be an issue. 有几个缺点-您正在检查AngularJS构建工件,并且AngularJS的更改与服务器端的更改混合在一起,但是我发现,除非服务器端和客户端团队完全独立,否则它就没有了。被证明是一个问题。

Best practice : 最佳实践 :

  1. Separation of Concerns. 关注点分离。 - Each module/component/controller should be independent from the other. -每个模块/组件/控制器应彼此独立。
  2. Mode of Development - Based on the role of team (full stack developer/UI separate /Backend separate. Folder structure changes 开发模式 -基于团队的角色(全栈开发人员/ UI分开/后端分开。文件夹结构发生变化
  3. Outstanding Style Guide Angular 1 出色的风格指南Angular 1
  4. Official style guide for Angular 2 Angular 2的官方样式指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM