注意:我不是一个合适的 python程序员...但我广泛使用python.我做的事情就像使用继承编写类,使用迭代器和理解等等.我的观点是我没有完全掌握语言,例如究竟是什么构成了python对象,为什么__init__.py
除了指定模块之外还需要等等关于Django,我已经编写了多应用程序站点(在SO的帮助下)并且非常喜欢Django的模板系统,块以及它们如何嵌套.现在我的应用程序是完全解耦和可重用的吗?这是这篇文章的主题.
我陈述了这个免责声明,因为许多Django资源似乎都认为人们知道这些事情.这使得对于仅仅是(子力量)用户的人来说理解一些文档和SO问题变得困难.所以请记住这个问题.
这些问题的灵感来自何时在django中使用startapp创建新应用程序的问题?通过@哈坎和答案给出@antti rasinen可链接到詹姆斯·贝内特2008 PYCON演示
Bennett演讲的几个要点是:
这引导我进入他提到的"项目耦合杀死重用"一节:
在这种情况下,"模块"只是一个由其他应用程序组成的应用程序?
(具有相关功能和共享模型的应用程序)
应用程序共享模型时应该怎么做?
在Barrett的幻灯片中,他暗示用户注册和用户配置文件是截然不同的,应该是不同的应用程序.(他当然声明配置文件与用户注册无关).
所以,如果我想要两者,我的项目会有两个应用程序,如:
即使应用程序user-profile
需要用户模型user-registration
?或者我制作一个应用程序(module
):
哪两个都包含?
(具有不同功能但共享模型的应用)
从问题1扩展示例,假设我的主应用程序(或主应用程序使用的其他应用程序)利用用户模型的某些方面(例如,如果它是聊天网站,则最近活跃的成员).
显然,我的主应用程序从用户模型中获取此信息.我的主应用程序现在是否捆绑在该user-app
模块下?
这可能不是最好的例子,但重点如下:
我有两个应用程序app-dependency
和app-needs-dependency
,其中每个应用程序做了一两件事,一件事......这仅仅是app-needs-dependency
从需要的信息app-dependency
.在这种情况下我该怎么做,如果其他所有内容app-needs-dependency
完全脱离app-dependency
(因此可以在其他项目中使用)?
(编写应用程序的灵活性)
现在我的网站上有几个应用程序.每个应用程序都做了一件事,并做得很好.在这种情况下,主应用程序用作登录页面/概述.
我希望我的所有其他应用程序都使用/继承主应用程序的静态和模板文件.
我在哪里存储所有静态文件和模板?在主应用程序中并将其设置为其他应用程序的默认值?或者应该将这些静态文件/模板(例如base.css
,base.html
)去?我是否为每个其他应用程序复制这些文件,因此即使这是多余的,它们也可以运行?
哪个让我的应用更灵活?
Python 上下文中的“模块”只是一个包含定义和语句的文件。因此,“包下的相关模块”实际上意味着“根据代码的用途将代码拆分为单独的文件”。
将其描述为“由其他应用程序组成的应用程序”会开始混淆 Django 的应用程序概念与 Python 的模块概念(如上所述,模块只是一个包含一些代码的文件)。
当应用程序共享模型时我该怎么办?
You should still try and stick to the "apps do one thing and do it well" maxim. In this case separate profile and registration apps seems like a good idea - because they have quite different functions. A registration app is going to contain the logic for allowing users to register on your site. A profile app is all about what information you will store about a user.
There is nothing wrong with these two apps having a relationship to each other - see below.
Let's say that my main app (or some other app that is used by the main app) utilizes some aspect of the user model (e.g. recently active members if it was a chat site). Clearly my main app gets this information from the user model. Does my main app now get bundled under the user-app?
No. It should still be a separate app, with links to the other app.
The user model is actually a good example. Django allows you to specify a custom user model that lets you store whatever additional data you want about a user.
Now, there are loads of third party apps out there that do things like registration, authentication, etc for users. They are designed to work with any user model, not just Django's default one. The way they do that is to use get_user_model()
wherever they need to reference the User
model, instead of directly importing django.contrib.auth.models.User
.
This means that you can use those third party apps with whatever user model you have defined for your own project.
Django's get_user_model()
utility is there to serve a very common use case. However the same principle can be extended to your own apps. If there is a dependency between your apps that you think should be swappable, then you can provide a way to swap it out - e.g., a setting/configuration that allows any other project using your app to specify an alternative.
Django 生态系统中有数百个此类可配置性的示例。例如,Django 本身附带了自己的django.contrib.auth
身份验证应用程序。但是,如果您想实现自己的身份验证逻辑,则不必自己再次重新实现整个身份验证应用程序(这将是一个巨大的痛苦)。相反,您指定一个身份验证后端,身份验证应用程序将使用该身份验证后端来进行身份验证。auth 应用程序旨在允许任何项目以最小的努力替换其核心功能。
因此,在您的main
应用程序中,您可以定义一个设置来控制profile
要使用的模型。这意味着,如果其他人想要使用不同的配置文件模型,他们只需更改此设置即可完成设置。它们不再与您的profile
应用程序绑定。
例如,假设您有一个main
应用程序,该应用程序的视图显示一些用户数据,但还提供了指向其他应用程序提供的注册视图的链接。您希望其他人能够使用该应用程序,无论他们使用什么注册应用程序。因此,您可以使该视图可重复使用,如下所示:
在main/views.py
:
from django.contrib.auth import get_user_model
from django.conf import settings
from django.urls import reverse
class UserDetailView(DetailView):
# First of all, we're using get_user_model so that a project
# can specify whatever user model it wants, and still use this
# view.
model = get_user_model()
def get_context_data(self, *args, *kwargs):
ctx = super().get_context_data(*args, **kwargs)
# We want to add a link to a registration view into this template context.
# But we want this to be configurable.
# Your REGISTRATION_URL would be something like 'profile:registration'
ctx['registration_link'] = reverse(settings.REGISTRATION_URL)
return ctx
Run Code Online (Sandbox Code Playgroud)
在这种情况下,主应用程序充当登陆页面/概述。我希望所有其他应用程序使用/继承主应用程序的静态文件和模板文件。我在哪里存储所有静态文件和模板?
您应该将模板存储在每个相应的应用程序中。如果您的主应用程序提供基本模板,那么这些模板应驻留在主应用程序中。
如果您的个人资料应用程序随后提供注册视图,则该模板应位于个人资料应用程序中。从主应用程序扩展基本模板没有任何问题 - 这可以很容易地被想要的项目覆盖。
可以对两个应用程序如何相互关联做出假设 - 只要您小心地允许推翻这些假设。
归档时间: |
|
查看次数: |
1536 次 |
最近记录: |