为什么不让你用我的产品,我的产品?你需要做的大致包括:
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路径上命名一个函数(通常是package.package.views.function_name),它就会被处理。就这么简单。所有这些“project”/“app”的东西都只是python包。
现在,你该怎么做?或者说,我该怎么做呢?好吧,如果你创建了一个重要的可重用功能,比如一个标记编辑器,那就是当你创建一个“顶级应用程序”时,它可能包含widget .py, fields.py, context_processors.py等——所有你可能想导入的东西。
类似地,如果你可以创建一个类似博客的东西,在安装中使用一种非常通用的格式,你可以把它包装在一个应用程序中,有自己的模板,静态内容文件夹等,并配置一个django项目的实例来使用该应用程序的内容。
没有硬性规定说您必须这样做,但这是框架的目标之一。事实上,所有的东西,包括模板,都允许你从一些共同的基础上包含,这意味着你的博客应该适合任何其他的设置,只需要照顾自己的部分。
但是,为了解决您的实际问题,是的,没有人说您不能使用顶级项目文件夹。这就是应用程序所做的,如果你真的想做,你可以做到。然而,我倾向于不这样做,原因如下:
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.
简而言之,有一个约定的原因和任何其他约定是一样的——当涉及到其他人处理您的项目时,它会有所帮助。如果我看到fields.py,我立即期望它的代码子类django的字段,而如果我看到inputtypes.py,我可能不清楚这意味着没有看它。