Django-项目范围内的通用资源 [英] Django - Project wide common resources

查看:35
本文介绍了Django-项目范围内的通用资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Django做第一个项目,我开始考虑重新组合所有通用资源,例如所有自定义的 template标签上下文处理器通用视图,...在一个应用程序中,就像这样:

I'm doing my first project with Django and I'm beginning to think about regrouping all common resources like all my custom template tags, context processors, generic views, ... in one application, like so:

- project
  + templates
  + media
  + static
  - common // <- the common resources directory
    - template_tags
        __init__.py
      views.py
      context_processors.py
  + app1
  + app2
  + app3

我知道应用程序应该是可重用的组件,在这种情况下,它将是最特定于项目的应用程序.

I know that an application is supposed to be a reusable component and in this case it would be the most project specific application.

这是一个好习惯吗?是否应将其视为我的设置中的应用程序?我应该如何进行?

Is it a good practice ? Should it be considered as an application in my settings ? How should I proceed ?

推荐答案

只要Django存在,可重用应用"的争论就一直在进行,并且在许多方面早于Django.无论有什么分量,您唯一要得到的答案就是对您的项目有意义.

The "reusable apps" debate has been going on as long as Django has existed, and predates Django in many ways. The only answer you're ever going to get with any weight is do what makes sense for your project.

可重复使用的应用程序"依赖其他应用程序运行并不罕见,因此这部分并不是问题.真正的目标始终是DRY(不要重复自己).如果您要开发的功能可以在您的项目甚至您可能从事的其他项目中的多个地方使用,那么它应该尽可能独立.

It's not uncommon for "reusable apps" to depend on other apps to function, so that part is really not a problem. The true goal is always DRY (don't repeat yourself). If you're developing functionality that can be used in multiple places in your project or even in other projects you might work on, then it should be as self-contained as possible.

我不会故意将所有模板标签等移动到一个应用中.但是,如果它是一个多用途的模板标签,那么将其放在自己的应用程序中实际上是有意义的.这实际上很普遍.但是,如果模板标记特定于特定应用程序,则它属于该应用程序.同样的口头禅适用于您提到的其余内容.

I wouldn't willy-nilly move all template tags and such into one app. But if it's a template tag that is multi-use, then it actually makes sense to put it in its own app. That's actually fairly common. However, if the template tag is specific to a particular app, it belongs with that app. The same mantra applies to the rest of the things you mentioned.

这篇关于Django-项目范围内的通用资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆