Django 项目工作目录结构的最佳实践 [英] Best practice for Django project working directory structure

查看:36
本文介绍了Django 项目工作目录结构的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道实际上没有唯一的正确方法.但是,我发现很难创建一个对每个开发人员和管理员来说都运行良好并保持干净的目录结构.github上的大多数项目都有一些标准的结构.但它并没有显示一种在 PC 上组织其他文件和所有项目的方法.

I know there is actually no single right way. However I've found that it's hard to create a directory structure that works well and remain clean for every developer and administrator. There is some standard structure in most projects on github. But it does not show a way to organize another files and all projects on pc.

在开发机器上组织所有这些目录的最方便的方法是什么?您如何命名它们,以及如何连接并将其部署到服务器?

What is the most convenient way to organize all these directories on development machine? How do you name them, and how do you connect and deploy this to server?

  • 项目(您正在进行的所有项目)
  • 源文件(应用程序本身)
  • 存储库的工作副本(我使用 git)
  • 虚拟环境(我更喜欢把它放在项目附近)
  • 静态根(用于编译的静态文件)
  • 媒体根目录(用于上传的媒体文件)
  • 自述文件
  • 许可证
  • 文件
  • 草图
  • examples(使用本项目提供的应用程序的示例项目)
  • 数据库(如果使用 sqlite)
  • 成功完成项目所需的任何其他东西

我想解决的问题:

  • 目录的好名称,以便其目的明确.
  • 将所有项目文件(包括 virtualenv)保存在一个地方,这样我就可以轻松地复制、移动、存档、删除整个项目或估算磁盘空间使用情况.
  • 为某些选定文件集(例如整个应用程序、存储库或 virtualenv)创建多个副本,同时保留我不想克隆的其他文件的单个副本.
  • 只需 rsyncing 选定的一个目录,即可将正确的文件集部署到服务器.

推荐答案

Django 有两种项目":我在 ~/projects/ 目录中的内容,两者的结构略有不同.:

There're two kind of Django "projects" that I have in my ~/projects/ directory, both have a bit different structure.:

  • 独立网站
  • 可插拔应用

主要是私人项目,但不一定是.它通常看起来像这样:

Mostly private projects, but doesn't have to be. It usually looks like this:

~/projects/project_name/

docs/               # documentation
scripts/
  manage.py         # installed to PATH via setup.py
project_name/       # project dir (the one which django-admin.py creates)
  apps/             # project-specific applications
    accounts/       # most frequent app, with custom user model
    __init__.py
    ...
  settings/         # settings for different environments, see below
    __init__.py
    production.py
    development.py
    ...
        
  __init__.py       # contains project version
  urls.py
  wsgi.py
static/             # site-specific static files
templates/          # site-specific templates
tests/              # site-specific tests (mostly in-browser ones)
tmp/                # excluded from git
setup.py
requirements.txt
requirements_dev.txt
pytest.ini
...

设置

主要设置是生产设置.其他文件(例如 staging.pydevelopment.py) 只需从 production.py 导入所有内容并仅覆盖必要的变量.

Settings

The main settings are production ones. Other files (eg. staging.py, development.py) simply import everything from production.py and override only necessary variables.

对于每个环境,都有单独的设置文件,例如.生产,发展.我还测试了一些项目(用于测试运行器),暂存(作为最终部署前的检查)和 heroku(用于部署到 heroku)设置.

For each environment, there are separate settings files, eg. production, development. I some projects I have also testing (for test runner), staging (as a check before final deploy) and heroku (for deploying to heroku) settings.

我宁愿直接在 setup.py 中指定要求.只有那些需要的我在 requirements_dev.txt 中有开发/测试环境.

I rather specify requirements in setup.py directly. Only those required for development/test environment I have in requirements_dev.txt.

某些服务(例如heroku)需要在根目录中有requirements.txt.

Some services (eg. heroku) requires to have requirements.txt in root directory.

在使用 setuptools 部署项目时很有用.它将 manage.py 添加到 PATH,这样我就可以直接(在任何地方)运行 manage.py.

Useful when deploying project using setuptools. It adds manage.py to PATH, so I can run manage.py directly (anywhere).

我曾经把这些应用放到project_name/apps/目录下并导入使用相对导入.

I used to put these apps into project_name/apps/ directory and import them using relative imports.

我将这些模板和静态文件放在全局模板/静态目录中,而不是在每个应用程序中.这些文件通常由不关心项目代码的人编辑结构或python.如果您是单独工作的全栈开发人员或在一个小团队中,您可以创建每个应用程序的模板/静态目录.这真的只是品味问题.

I put these templates and static files into global templates/static directory, not inside each app. These files are usually edited by people, who doesn't care about project code structure or python at all. If you are full-stack developer working alone or in a small team, you can create per-app templates/static directory. It's really just a matter of taste.

这同样适用于语言环境,尽管有时创建单独的语言环境目录很方便.

The same applies for locale, although sometimes it's convenient to create separate locale directory.

测试通常最好放在每个应用程序中,但通常有很多集成/功能测试可测试更多应用程序协同工作,因此具有全局性测试目录确实有意义.

Tests are usually better to place inside each app, but usually there is many integration/functional tests which tests more apps working together, so global tests directory does make sense.

项目根目录中有临时目录,排除在 VCS 之外.它习惯了在开发过程中存储媒体/静态文件和 sqlite 数据库.一切都在tmp 可以随时删除,没有任何问题.

There is temporary directory in project root, excluded from VCS. It's used to store media/static files and sqlite database during development. Everything in tmp could be deleted anytime without any problems.

我更喜欢 virtualenvwrapper 并将所有 venvs 放入 ~/.venvs 目录,但你可以将它放在 tmp/ 中以保持在一起.

I prefer virtualenvwrapper and place all venvs into ~/.venvs directory, but you could place it inside tmp/ to keep it together.

我已经为此设置创建了项目模板,django-start-template

I've created project template for this setup, django-start-template

该项目的部署如下:

source $VENV/bin/activate
export DJANGO_SETTINGS_MODULE=project_name.settings.production
git pull
pip install -r requirements.txt

# Update database, static files, locales
manage.py syncdb  --noinput
manage.py migrate
manage.py collectstatic --noinput
manage.py makemessages -a
manage.py compilemessages

# restart wsgi
touch project_name/wsgi.py

您可以使用 rsync 而不是 git,但您仍然需要运行批处理命令来更新您的环境.

You can use rsync instead of git, but still you need to run batch of commands to update your environment.

最近,我制作了 django-deploy 应用程序,它允许我运行单个管理命令来更新环境,但我只将它用于一个项目,我仍在试验它.

Recently, I made django-deploy app, which allows me to run single management command to update environment, but I've used it for one project only and I'm still experimenting with it.

我放置在全局 templates/ 目录中的模板草案.我想可以在项目根目录中创建文件夹sketches/,但还没有使用它.

Draft of templates I place inside global templates/ directory. I guess one can create folder sketches/ in project root, but haven't used it yet.

这些应用通常准备作为开源发布.我举了例子下面来自 django-forme

These apps are usually prepared to publish as open-source. I've taken example below from django-forme

~/projects/django-app/

docs/
app/
tests/
example_project/
LICENCE
MANIFEST.in
README.md
setup.py
pytest.ini
tox.ini
.travis.yml
...

目录名称很清楚(我希望).我将测试文件放在 app 目录之外,但这真的无所谓.提供READMEsetup.py 很重要,这样可以通过pip 轻松安装包.

Name of directories is clear (I hope). I put test files outside app directory, but it really doesn't matter. It is important to provide README and setup.py, so package is easily installed through pip.

这篇关于Django 项目工作目录结构的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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