Django:将项目分解为应用程序的最佳做法 [英] Django: best practice for splitting up project into apps

查看:162
本文介绍了Django:将项目分解为应用程序的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很挣扎着这个整个应用程序的想法。我读了很多教程和风格指南,我知道我应该尝试创建专门的应用程序,这只是一件事。在查看一些简单的教程项目时,这一切都是有意义的,但一旦达到复杂的现实生活项目,我发现自己无法确定如何在不同的应用程序之间绘制线条。



其中一个问题是,我想要一个网站(或多个网站),用户看到很多不同的东西。应该来自不同的应用程序的东西,当遵循应用程序设计规则。我怎么会意识到这样的事情?我的第一个想法是创建一个名为 ui 的应用程序,它只是处理实际导致模板的所有视图,所有其他应用程序都提供了模型和帮助功能。但是我担心, ui 应用程序将变得很大。



给你一个小例子:让我想要一个网站,用户可以执行以下任务:




  • 选择主题

  • 为所选主题设置一些选项

  • 上传与其帐户相关联的文件

  • 将一些上传的文件分配到主题

  • 记录一些与主题相关的音频



现在,我将创建三个应用程序: / p>


  1. 主题(包含主题模型和一些相关模型)

  2. 资源(包含资源模型,处理上传)

  3. 音频(处理所有音频录制和处理内容)

但是,我需要某种 main ui 应用程序来处理这些应用程序的交互方式并创建实际的网站,所有应用程序都在某种程度上涉及。



那么这样做有什么正确的方式吗?还是有什么可以使用的模式?我也会感谢有关这个话题的很好的资源链接,尽管我已经读了不少。

解决方案

您只需确保您的结构对您有意义。



没有必要为绑定到项目逻辑的另一部分的每个功能创建一个新的应用程序。



可重复使用的应用程序是一个完全不同的故事,他们的代码在某种程度上应该不会实现。



查看 Django的结构灵感



可能的布局为您的示例:

  project_root / 
project /
__init__.py
settings.py
urls.py
templates /
app1 /#override stuff
static /
media /
app1 /
__init__.py
admin /#as a package
__init__.py
subjects.py
resources.py
#etc
models /#as a package
subjects.py
resources.py
#etc
管理者/
__init__.py
主题.py
resources.py
#etc
services /
__init__.py
audio.py#upload handler etc
views /
__init__ .py
subjects.py
urls /
__init__.py
subjects.py
templates /
app1 /
subject_list.html#override在项目级别
static /
app1 /
css /
subject.css#在项目级别覆盖
app2 /
__init__.py
models.py#拥有会员模型或任何您需要的
manage.py


I'm really struggling with this whole app-idea. I read a lot of tutorials and style guides and I know I should try to create specialized apps, that do exactly one thing. This all makes sense when looking at some simple tutorial project but as soon as it gets to a complex real life project, I find myself unable to determine how I should draw the lines between different apps.

One of the problems is, that I want to have one site (or multiple sites) where the user sees a lot of different stuff. Stuff that should be from different apps, when following the app design rules. How would I realize something like this? My first idea was to create one app called ui, that just handles ALL the views the actually lead to a template and all the other apps provide the models and helperfunctions. But I fear that the ui app will become way to big.

To give you a small example: Lets I want to have a site where the user can do the following tasks:

  • Select a subject
  • set some options to the selected subject
  • upload files that are associated with his account
  • assign some of the uploaded files to the subject
  • record some audio which will be related to the subject

Right now, I would create three apps:

  1. subjects (contains the subject model and some related models)
  2. resources (contains the resource model, handles uploads)
  3. audio (handles all the audio recording and processing stuff)

But then, I would need some kind of main or ui app to handle how these apps interact and to create the actual site, where all the apps are somehow involved.

So, is there any "right" way to do this? Or are there any patterns that I can use? I would also appreciate links to good resources about this topic, even though I already read quite a few.

解决方案

You just need to make sure your structure makes sense to you.

There's no requirement to create a new app for every feature that is bound to another part of the project's logic.

Reusable apps are a whole different story, their code should be unaware of the implementation to some extend.

Take a look at Django's structure for inspiration

A possible layout for your example:

project_root/
    project/
        __init__.py
        settings.py
        urls.py
        templates/
            app1/  # override stuff
        static/
        media/
    app1/
        __init__.py
        admin/  # as a package
            __init__.py
            subjects.py
            resources.py
            # etc
        models/  # as a package
            subjects.py
            resources.py
            # etc
        managers/
            __init__.py
            subjects.py
            resources.py
            # etc
        services/
            __init__.py
            audio.py  # upload handler etc
        views/
            __init__.py
            subjects.py
        urls/
            __init__.py
            subjects.py
        templates/
            app1/
                subject_list.html  # override at project level
        static/
            app1/
                css/
                    subject.css  # override at project level
    app2/
        __init__.py
        models.py  # holds a Member model or whatever you require
    manage.py

这篇关于Django:将项目分解为应用程序的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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