問題描述
我準(zhǔn)備使用 Django 構(gòu)建一個相當(dāng)復(fù)雜的產(chǎn)品".我將避免在這種情況下使用術(shù)語項目"和應(yīng)用程序",因為我不清楚它們在 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.
項目可以有許多應(yīng)用程序.應(yīng)用程序可以在許多項目之間共享.很好.
Projects can have many apps. Apps can be shared among many projects. Fine.
我沒有重新發(fā)明博客或論壇 - 我看不到我的產(chǎn)品的任何部分在任何情況下都可以重復(fù)使用.直觀地說,我將其稱為應(yīng)用程序".然后我是否在一個app"文件夾中完成所有工作?
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?
如果是這樣...就 Django 的 project.app
命名空間而言,我傾向于使用 myproduct.myproduct
,但當(dāng)然這是不允許的(但我正在構(gòu)建的應(yīng)用程序是我的項目,我的項目是一個應(yīng)用程序!).因此,我相信也許我應(yīng)該通過為每個重要"模型構(gòu)建一個應(yīng)用程序來接近 Django,但我不知道在我的架構(gòu)中在哪里繪制邊界以將其分成應(yīng)用程序 - 我有很多具有相對復(fù)雜關(guān)系的模型.
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...
推薦答案
是什么阻止了你使用 myproduct.myproduct
?你需要做的大致包括這樣做:
What is to stop you using myproduct.myproduct
? What you need to achieve that roughly consists of doing this:
django-admin.py startproject myproduct
cd myproduct
mkdir myproduct
touch myproduct/__init__.py
touch myproduct/models.py
touch myproduct/views.py
等等.如果我說 views.py
不必被稱為 views.py
會有幫助嗎?如果您可以在 python 路徑上命名一個函數(shù)(通常是 package.package.views.function_name),它將被處理.就那么簡單.所有這些項目"/應(yīng)用程序"的東西都只是 python 包.
and so on. Would it help if I said views.py
doesn't have to be called views.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.
現(xiàn)在,你應(yīng)該怎么做?或者更確切地說,我該怎么做?好吧,如果你創(chuàng)建了一個重要的可重用功能,比如標(biāo)記編輯器,那就是當(dāng)你創(chuàng)建一個頂級應(yīng)用程序"時,它可能包含 widgets.py
、fields.py
、context_processors.py
等 - 您可能想要導(dǎo)入的所有內(nèi)容.
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.
類似地,如果您可以創(chuàng)建類似博客的內(nèi)容,其格式在安裝過程中非常通用,您可以將其包裝在一個應(yīng)用程序中,使用它自己的模板、靜態(tài)內(nèi)容文件夾等,并配置一個 django 項目的實例使用該應(yīng)用的內(nèi)容.
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.
沒有硬性規(guī)定必須這樣做,但這是框架的目標(biāo)之一.事實上,包括模板在內(nèi)的所有內(nèi)容都允許您從一些共同的基礎(chǔ)中包含在內(nèi),這意味著您的博客應(yīng)該緊貼任何其他設(shè)置,只需照顧好它自己的部分.
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.
但是,為了解決您的實際問題,是的,沒有什么說您不能使用頂級項目文件夾.這就是應(yīng)用程序所做的,如果你真的愿意,你可以做到.然而,我傾向于不這樣做,原因如下:
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:
- Django 的默認(rèn)設(shè)置不這樣做.
- 通常,我想創(chuàng)建一個主應(yīng)用程序,所以我創(chuàng)建了一個,通常稱為
website
.但是,以后我可能想為這個站點開發(fā)原始功能.為了使其可移動(無論我是否曾經(jīng)這樣做),我傾向于創(chuàng)建一個單獨(dú)的目錄.這也意味著我可以通過從配置中取消鏈接該包并刪除文件夾來刪除所述功能,而不是從全局 urls.py 文件夾中復(fù)雜地刪除正確的 url. - 很多時候,即使我想讓某物獨(dú)立,也需要在我照顧它/讓它獨(dú)立的同時有地方住.基本上是上述情況,但對于我確實打算通用的東西.
- 我的頂級文件夾通常包含一些其他內(nèi)容,包括但不限于 wsgi 腳本、sql 腳本等.
- django 的 管理擴(kuò)展 依賴于子目錄.因此,適當(dāng)?shù)孛怯幸饬x的.
- Django's default setup doesn't do it.
- Often, I want to create a main app, so I create one, usually called
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. - Very often, even when I want to make something independent, it needs somewhere to live whilst I look after it / make it independent. Basically the above case, but for stuff I do intend to make generic.
- My top level folder often contains a few other things, including but not limited to wsgi scripts, sql scripts etc.
- django's management extensions rely on subdirectories. So it makes sense to name packages appropriately.
簡而言之,存在約定的原因與任何其他約定相同 - 當(dāng)涉及到其他人處理您的項目時,它會有所幫助.如果我看到 fields.py
我會立即期望其中的代碼子類化 django 的字段,而如果我看到 inputtypes.py
我可能不看就不太清楚這意味著什么在它.
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 see inputtypes.py
I might not be so clear on what that means without looking at it.
這篇關(guān)于Django:“項目"與“應(yīng)用程序"相比的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!