Python Django 世界中的项目和应用程序有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19350785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:32:42  来源:igfitidea点击:

What’s the difference between a project and an app in Django world?

pythondjango

提问by Ali Issa

I am creating my first real website using Django but I am still struggling to understand the diff between a project and an app.

我正在使用 Django 创建我的第一个真正的网站,但我仍在努力理解项目和应用程序之间的差异。

For example, My website is a sports news website which will contain sections like articles, ranking tables and "fixtures and results", my question is should each one of these sections be in a separate app inside a whole project or not? what is the best practice in this situation?

例如,我的网站是一个体育新闻网站,其中将包含文章、排名表和“赛程和结果”等部分,我的问题是这些部分中的每一个都应该在整个项目中的单独应用程序中吗?在这种情况下,最佳做法是什么?

采纳答案by John Mee

A projectrefers to the entire application and all its parts.

一个项目是指整个应用程序及其所有部件。

An apprefers to a submodule of the project. It's self-sufficient and not intertwined with the other apps in the project such that, in theory, you could pick it up and plop it down into another project without any modification. An apptypically has its own models.py(which might actually be empty). You might think of it as a standalone python module. A simple project might only have one app.

一个应用程序是指项目的子模块。它是自给自足的,不会与项目中的其他应用程序交织在一起,因此理论上,您可以将其捡起并将其放入另一个项目中而无需任何修改。一个应用程序通常都有自己的models.py(这实际上可能是空的)。您可能会认为它是一个独立的 Python 模块。一个简单的项目可能只有一个应用程序。

For your example, the projectis the whole website. You might structure it so there is an appfor articles, an appfor ranking tables, and an appfor fixtures and results. If they need to interact with each other, they do it through well-documented public classes and accessor methods.

对于您的示例,该项目是整个网站。所以还有一个你可能构建它的应用程序的文章,一个应用程序的排名表,以及应用程序的固定装置和结果。如果它们需要相互交互,它们会通过记录良好的公共类和访问器方法来实现。

The main thing to keep in mind is this level of interdependence between the apps. In practice it's all one project, so there's no sense in going overboard, but keep in mind how co-dependent two apps are. If you find one app is solving two problems, split them into two apps. If you find two apps are so intertwined you could never reuse one without the other, combine them into a single app.

要记住的主要事情是应用程序之间的这种相互依赖程度。在实践中,这都是一个项目,因此过火是没有意义的,但请记住两个应用程序的相互依赖程度。如果您发现一个应用程序正在解决两个问题,请将它们拆分为两个应用程序。如果您发现两个应用程序如此交织在一起,以至于没有另一个应用程序就无法重复使用,请将它们合并为一个应用程序。

回答by Paulo Bu

Ideally, your project should be composed by apps. That's why when using the command line, you create a project, an later on, add apps to that project.

理想情况下,您的项目应该由 apps 组成。这就是为什么在使用命令行时,您创建一个项目,然后将应用程序添加到该项目。

Apps, aims to bring modularityto your project. For example, if you build an articles app, ideally, you can use it in your sports news project, and re-use it in a new project which requires it with minimum or no modification to its settings-- say a blog project, for example.

应用程序旨在为您的项目带来模块化。例如,如果您构建了一个articles app理想情况下,您可以在您的体育新闻项目中使用它,并在需要对其进行最少修改或无需修改的新项目中重新使用它settings——例如,一个博客项目。

Apps are piece of software meant to be reused. Your project stands only for your very specific needs.

应用程序是一种可以重复使用的软件。您的项目仅代表您非常特定的需求。

Take a look at Django Project Structure. It may give you some insight in the best practice of organizing your Django project.

看看Django 项目结构。它可能会让您对组织 Django 项目的最佳实践有所了解。

There are also several blog posts searchable on Google that address this topic:

还有一些可在 Google 上搜索到的博客文章与此主题相关:

回答by Ahtisham

Lets understand Project and App in Django with this realistic example:

让我们通过这个真实的例子来理解 Django 中的 Project 和 App:

Say you are building an online shopping site (e-commerce site) in Django:

假设您正在 Django 中构建一个在线购物网站(电子商务网站):

Project:

项目:

Its simply name of your website. Django will create a python packageand give it a name that you have provided. lets say we name it my_shopping_site.

它只是您网站的名称。Django 将创建一个python 包并给它一个您提供的名称。假设我们将其命名为 my_shopping_site

You can create a project in Django with this command

您可以使用此命令在 Django 中创建一个项目

python manage.py startproject my_shopping_site

This will create my_shopping_sitedirectory in your working directory and the structure will look like this:

这将my_shopping_site在您的工作目录中创建目录,结构如下所示:

my_shopping_site/
   manage.py
   my_shopping_site/    # package
        __init__.py     # indication of package
        settings.py     # module 1
        urls.py         # module 2
        wsgi.py         # module 3

Apps:

应用:

Its those little components that together make up your project. They are the features of your project. In our case (shopping site) it would be:

它的那些小组件共同构成了您的项目。它们是您项目的功能。在我们的情况下(购物网站),它将是:

  • Cart:- Which would have a logic for user selected items for purchase.

  • Products:- Which would have a logic for products that the site is selling.

  • Profile:- Which would have a logic for user information.

    -----------------------------------------------------------
    my_shopping_site              Products     Profile     Cart
    -----------------------------------------------------------
    
  • 购物车:- 这将具有用户选择的购买项目的逻辑。

  • 产品:- 对于网站正在销售的产品,这将有一个逻辑。

  • 配置文件:- 这将具有用户信息的逻辑。

    -----------------------------------------------------------
    my_shopping_site              Products     Profile     Cart
    -----------------------------------------------------------
    

and you can create these apps with these commands:

您可以使用以下命令创建这些应用程序:

python manage.py startapp cart
python manage.py startapp products
python manage.py startapp profile

The structure would look like this:

结构如下所示:

my_shopping_site/   # project name
      manage.py
      products/          # app 1
      cart/              # app 2 
      profile/           # app 3
      my_shopping_site/

each app focus on a single logical piece of your project.

每个应用程序都专注于项目的一个逻辑部分。