Django - Project wide common resources

蓝咒 提交于 2020-02-02 15:29:14

问题


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 ?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/6661583/django-project-wide-common-resources

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!