Django项目层次结构/组织 [英] Django project hierarchy/organization

查看:162
本文介绍了Django项目层次结构/组织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的新手,并开始了一个项目,我想以正确的方式做到。

I am new to Django and starting a project, and I would like to do it the right way.

我想知道你认为最好的做法

I would like to know what you think is best practice for organizing a project.

这里有一些问题:


  • 如何我将静态资源与Python代码分开,以免我通过Django浪费时间来处理静态内容?

  • 由于应用程序是可重复使用的模块,因此它们对项目并不太紧密,那么它们应该位于项目目录中,还是在另一个包含所有自制应用程序的目录中?

  • 模板被认为是静态或动态内容?

这是我当前的文件层次结构:

Here is my current file hierarchy:

webapps/
    myproject/
        apache/
        bin/
        lib/
        templates/
            app1/
            app2/
        src/
            app1/
            app2/
            __init.py
            settings.py
            urls.py
            manage.py
        myproject.wsgi
    admin/
    static/
        css/
        img/

你觉得怎么样?
什么会更好?

What do you think? What would be better?

谢谢!

推荐答案

您的目录结构也可能取决于您使用的django的版本。如果您使用django 1.3,处理静态内容已略有变化。您的模板也可以单独安排。

Your directory structure could also depend on what version of django that you're using. If you're using django 1.3, handling static content has changed slightly. Your templates could also be arranged separately.

以下仅适用于django 1.3。

应用程序目录中:

...
app1/
    static/
        app1/
    templates/
        app1/
    models.py
    ...
    views.py

如果您使用新的 django.contrib.staticfiles 应用程序,您的设置可能如下所示:

If you use the new django.contrib.staticfiles application, your settings may look something like this:

MEDIA_ROOT = path.join(ROOT_PATH,'uploaded_media/')
MEDIA_URL = '/uploaded_media/'
# static content is collected here, and served from here, but don't add stuff manually here! add to staticfiles_dirs
STATIC_ROOT = path.join(ROOT_PATH, 'collected_static/')
ADMIN_MEDIA_PREFIX = '/static/admin/'
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
    path.join(ROOT_PATH, 'src/extra_static/'),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

同样,您的模板可以直接从 INSTALLED_APP 加载:

Similarly, your templates can be loaded directly from an INSTALLED_APP:

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader'
)

TEMPLATE_DIRS = (
    path.join(ROOT_PATH,'src/templates/'),
)

上述两个策略意味着模板和静态内容可以在其特定的应用程序中生活d irectories。在开发中,使用 contrib.staticfiles ,可以直接从应用程序文件夹中提供静态内容。在生产中,有一个管理命令将所有的应用程序目录静态内容收集到 / path / to / project / gather_static / ,您可以将Web服务器指向该目录提供静态内容。

The two strategies above mean that templates and static content can live within their specific app directories. In Development, using contrib.staticfiles, static content can be served directly from your application folders. In production, there is a management command to collect all the app directory static content to /path/to/project/collected_static/, and you can point your web server at that directory to serve static content.

对于预打包的库,使用virtualenv和pip是一个好主意。否则,我希望将库保存在项目根目录下的 lib 目录中。它引用源,模板和静态内容非常方便,而不是安装到 site-packages (尤其是在不使用virtualenv时)。

For pre-packaged libraries, using virtualenv and pip is a great idea. Otherwise, I like to keep libraries in a lib directory within the project root directory. It makes referencing the source, templates, and static content extremely convenient, rather than installing to site-packages (especially when not using virtualenv).

因此,重新安排您的项目结构:

So, re-arranging your project structure:

webapps/
    myproject/
        apache/
        bin/
        lib/
        collected_static/
        uploaded_media/
        myproject.wsgi
        src/
            templates/ # lib template overrides and site wide templates
                base.html
                lib_1/
                    nav.html
            extra_static/
                lib_1/ # libs that dont support django 1.3 static
                    js/
                    css/
            settings.py
            settingslocal.py # keep developer specific settings here
            urls.py
            manage.py
            app1/
                __init.py
                static/
                    app1/
                        js/
                        css/
                templates/
                    app1/

这篇关于Django项目层次结构/组织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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