Django:“项目”与“应用程序”
我有一个相当复杂的“产品”,我正准备使用 Django 构建。我将避免在这种情况下使用术语“项目”和“应用程序”,因为我不清楚它们在 Django 中的具体含义。
项目可以有许多应用程序。应用程序可以在许多项目之间共享。美好的。
我没有重新发明博客或论坛 - 我不认为我的产品的任何部分可以在任何情况下重复使用。直觉上,我将其称为“应用程序”。那么我是否可以在一个“app”文件夹中完成所有工作?
如果是这样...就 Django 的 project.app
命名空间而言,我倾向于使用 myproduct.myproduct
,但这当然不是'不允许(但我正在构建的应用程序是我的项目,而我的项目是一个应用程序!)。因此,我相信也许我应该通过为每个“重要”模型构建一个应用程序来接近 Django,但我不知道在哪里绘制模式中的边界以将其分离为应用程序 - 我有很多关系相对复杂的模型。
我希望有一个共同的解决方案......
I have a fairly complex "product" I'm getting ready to build using Django. I'm going to avoid using the terms "project" and "application" in this context, because I'm not clear on their specific meaning in Django.
Projects can have many apps. Apps can be shared among many projects. Fine.
I'm not reinventing the blog or forum - I don't see any portion of my product being reusable in any context. Intuitively, I would call this one "application." Do I then do all my work in a single "app" folder?
If so... in terms of Django's project.app
namespace, my inclination is to use myproduct.myproduct
, but of course this isn't allowed (but the application I'm building is my project, and my project is an application!). I'm therefore lead to believe that perhaps I'm supposed to approach Django by building one app per "significant" model, but I don't know where to draw the boundaries in my schema to separate it into apps - I have a lot of models with relatively complex relationships.
I'm hoping there's a common solution to this...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一旦您不再使用
startproject
和startapp
,就没有什么可以阻止您在同一个 Python 包中组合“项目”和“应用程序”。项目实际上只不过是一个settings
模块,而应用程序实际上只不过是一个models
模块 - 其他所有内容都是可选的。对于小型网站,具有以下内容是完全合理的:
Once you graduate from using
startproject
andstartapp
, there's nothing to stop you from combining a "project" and "app" in the same Python package. A project is really nothing more than asettings
module, and an app is really nothing more than amodels
module—everything else is optional.For small sites, it's entirely reasonable to have something like:
在我开始使用 django 后不久,我就在某处读到了这个想法,我发现我经常问自己这个问题,这对我很有帮助。
您的应用程序不必可重用,它们可以相互依赖,但它们应该做一件事。
I read this thought somewhere soon after I've started to work with django and I find that I ask this question of myself quite often and it helps me.
Your apps don't have to be reusable, they can depend on each other, but they should do one thing.
是什么阻止您使用
myproduct.myproduct
?您需要实现的目标大致包括执行以下操作:等等。如果我说
views.py
不必称为views.py
会有帮助吗?如果您可以在 python 路径上命名一个函数(通常是 package.package.views.function_name),它将被处理。就这么简单。所有这些“项目”/“应用程序”的东西只是 python 包。现在,你该怎么做呢?或者更确切地说,我该怎么做?好吧,如果您创建了一个重要的可重用功能,例如标记编辑器,那么您就创建了一个“顶级应用程序”,其中可能包含
widgets.py
、fields.py、
context_processors.py
等 - 您可能想要导入的所有内容。同样,如果您可以以跨安装的通用格式创建博客之类的内容,则可以将其包装在应用程序中,并使用其自己的模板、静态内容文件夹等,并配置 django 项目的实例以使用它应用程序的内容。
没有硬性规定要求您必须这样做,但这是该框架的目标之一。事实上,所有内容(包括模板)都允许您从某个公共基础中包含内容,这意味着您的博客应该能够紧密地融入任何其他设置,只需照顾好自己的部分即可。
但是,为了解决您的实际问题,是的,没有什么表明您不能使用顶级项目文件夹。 这就是应用程序的作用,如果您确实愿意,也可以做到。然而,出于以下几个原因,我倾向于不这样做:
网站
的应用程序。然而,稍后我可能想专门为这个网站开发原始功能。为了使其可移动(无论我是否曾经这样做过),我倾向于创建一个单独的目录。这也意味着我可以通过从配置中取消该包的链接并删除该文件夹来删除所述功能,而不是从全局 urls.py 文件夹中复杂地删除正确的 url。简而言之,存在约定的原因与任何其他约定相同 - 当涉及到与您的项目一起工作的其他人时,它会有所帮助。如果我看到
fields.py
我立即期望其中的代码子类化 django 的字段,而如果我看到inputtypes.py
我可能不太清楚这意味着什么而不看在它。What is to stop you using
myproduct.myproduct
? What you need to achieve that roughly consists of doing this:and so on. Would it help if I said
views.py
doesn't have to be calledviews.py
? Provided you can name, on the python path, a function (usually package.package.views.function_name) it will get handled. Simple as that. All this "project"/"app" stuff is just python packages.Now, how are you supposed to do it? Or rather, how might I do it? Well, if you create a significant piece of reusable functionality, like say a markup editor, that's when you create a "top level app" which might contain
widgets.py
,fields.py
,context_processors.py
etc - all things you might want to import.Similarly, if you can create something like a blog in a format that is pretty generic across installs, you can wrap it up in an app, with its own template, static content folder etc, and configure an instance of a django project to use that app's content.
There are no hard and fast rules saying you must do this, but it is one of the goals of the framework. The fact that everything, templates included, allows you to include from some common base means your blog should fit snugly into any other setup, simply by looking after its own part.
However, to address your actual concern, yes, nothing says you can't work with the top level project folder. That's what apps do and you can do it if you really want to. I tend not to, however, for several reasons:
website
. However, at a later date I might want to develop original functionality just for this site. With a view to making it removable (whether or not I ever do) I tend to then create a separate directory. This also means I can drop said functionality just by unlinking that package from the config and removing the folder, rather than a complex delete the right urls from a global urls.py folder.In short, the reason there is a convention is the same as any other convention - it helps when it comes to others working with your project. If I see
fields.py
I immediately expect code in it to subclass django's field, whereas if I seeinputtypes.py
I might not be so clear on what that means without looking at it.我发现以下关于 django 应用程序和项目的博客文章非常有用:
原则上,使用 django 你有很大的自由度组织产品的源代码。
I've found the following blog posts very useful about django applications and projects:
In principle, you have a lot of freedom with django for organizing the source code of your product.
没有什么是不允许的。这是你的项目,没有人限制你。建议保留合理的名称。
在一般的 django 项目中,有许多应用程序(contrib 应用程序)在每个项目中都会用到。
假设您的项目只执行一项任务并且只有一个应用程序(我将其命名为
main
,因为该项目围绕它展开并且几乎不可插入)。这个项目通常也仍然使用一些其他应用程序。现在,如果您说您的项目仅使用一个应用程序 (
INSTALLED_APPS='myproduct'
),那么将项目定义为project.app 有什么用
,我认为你应该考虑一些要点:project
就应用程序中完成的大部分工作而言,我认为大多数 django 项目都是如此。
There is nothing like not allowed. Its your project, no one is restricting you. It is advisable to keep a reasonable name.
In a general django project there are many apps (contrib apps) which are used really in every project.
Let us say that your project does only one task and has only a single app (I name it
main
as thethe project revolves around it and is hardly pluggable). This project too still uses some other apps generally.Now if you say that your project is using just the one app (
INSTALLED_APPS='myproduct'
) so what is use ofproject
defining the project asproject.app
, I think you should consider some points:As far as most of the work being done in the app is concerned, I think that is the case with most of django projects.
这里 Django 创建者自己指出了这种差异。
我认为考虑应用程序必须在其他项目中可重用是好的。考虑 Django 中的应用程序提供现代 Web 应用程序也是一种好方法。
想象一下,您正在基于 JavaScript 创建大型动态网络应用程序。
然后您可以在 django 应用程序中创建名为“FrontEnd”的应用程序<--在 Thins 应用程序中您将显示内容。
然后您创建一些后端应用程序。例如,名为“评论”的应用程序将存储用户评论。而“评论”App本身不会显示任何内容。它将只是您的动态 JS 网站 AJAX 请求的 API。
通过这种方式,您可以随时重复使用您的“评论”应用程序。您可以将其开源,而无需开源整个项目的源代码。并且您可以保持项目的清晰逻辑。
Here Django creators points out that difference themselves.
I think that thinking about Apps as they have to be reusable in other projects is good. Also a good way of thinking about Apps in Django provide modern web applications.
Imagine that you are creating big dynamic web app basing on JavaScript.
You can create then in django App named e.g "FrontEnd" <-- in thins app you will display content.
Then you create some backend Apps. E.g App named "Comments" that will store user comments. And "Comments" App will not display anything itself. It will be just API for AJAX requests of your dynamic JS website.
In this way you can always reuse your "Comments" app. You can make it open source without opening source of whole project. And you keep clean logic of your project.