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

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

问题描述

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



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




  • 项目(您正在处理的所有项目)

  • 源文件(应用程序本身)

  • 存储库的工作副本(我使用git)

  • 虚拟环境(我喜欢把它放在项目附近)

  • 静态根(用于编译的静态文件)

  • 媒体根(用于上传的媒体文件) li>
  • README

  • 许可

  • 文件

  • 草图

  • 示例(使用此项目提供的应用程序的示例项目)

  • 数据库(以防sqlite使用)

  • 您通常需要在项目上成功工作的其他任何东西



我想解决的问题:




  • 目录的好名称,目的很清楚。

  • 将所有项目文件(包括virtualenv)保存在一个地方,我可以很容易地复制,移动,存档,删除整个项目或估计磁盘空间使用。

  • 创建多个选定的文件集的副本,如整个应用程序,存储库或virtualenv,同时保留其他文件的单个副本我不想克隆。

  • 只需通过rsyncing选定的一个目录将正确的文件集合部署到服务器。


解决方案

在我的〜/ projects / 目录中有两种Django项目 ,都有一点不同的结构:




  • 独立网站

  • 可插拔应用程序



独立网站



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

 〜/ projects / project_name / 

docs /#documentation
scripts /
manage.py#通过setup.py
project_name /#project dir(django-admin.py创建的项目)安装到PATH
apps /#项目特定应用程序
帐户/#最常用的应用程序,具有自定义用户模型
__init__.py
...
设置/#设置为不同的环境,见下文
__init__.py
production.py
development.py
...

__init__.py#包含项目版本
urls.py
wsgi.py
static /#site-specific static files
templates /#site-specific templates
tests /#site-specific tests(大部分是浏览器中的)
tmp /# git
setup.py
requirements.txt
requirements_dev.txt
pytest.ini
...



设置



主要设置是生产设置。其他文件(例如 staging.py
development.py )只需从 production.py 并仅覆盖必要的变量。



对于每个环境,都有单独的设置文件,例如。生产,
开发。我有一些项目,我也测试(测试跑步者),分期
(作为最终部署前的支票)和英雄(部署到heroku)设置。



要求



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



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



安装。



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



项目特定应用程序



我曾经把这些应用程序 project_name / apps / 目录,并使用相对导入导入



模板/静态/区域设置/测试文件



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



同样适用于语言环境,虽然有时它可以方便地创建单独的语言环境目录。



测试通常更好地放置在每个应用程序中,但通常有许多
集成/功能测试来测试更多的应用程序在一起工作,所以全球
测试目录是有道理的。



Tmp目录



项目根目录中有临时目录,从VCS中排除。它在开发过程中用于
存储媒体/静态文件和sqlite数据库。
tmp中的所有内容都可以随时删除而无任何问题。



Virtualenv



我更喜欢 virtualenvwrapper 并将所有venvs放入〜/ .venvs 目录,
,但您可以将其放在 tmp / 保持在一起。



项目模板



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



部署



该项目的部署如下:

 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 




#更新数据库,静态文件,locales
manage.py syncdb --noinput
manage.py migrate
manage.py collectstatic --noinput
manage.py makemessages -a
manage.py compilemessages

#restart wsgi
to uch project_name / wsgi.py

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



我做了 [django-deploy] [2] 应用程序,它允许我运行单一管理命令来更新环境,但是我已经用它了一个项目,



草图和草稿



我在全球范围内放置的模板草案 templates / 目录。我想可以在项目根目录下创建文件夹草图/ ,但尚未使用。



可插拔应用程序



这些应用程序通常准备作为开源发布。我从 django-forme 以下的示例

 〜/ projects / django-app / 

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

目录名称是清楚的(我希望)。我把测试文件放在应用程序目录下,
,但确实没关系。提供 README setup.py 非常重要,所以通过 pip可轻松安装软件包


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?

  • projects (all projects that your are working on)
  • source files (the application itself)
  • working copy of repository (I use git)
  • virtual environment (I prefer to place this near the project)
  • static root (for compiled static files)
  • media root (for uploaded media files)
  • README
  • LICENSE
  • documents
  • sketches
  • examples (an example project that uses the application provided by this project)
  • database (in case sqlite is used)
  • anything else that you usually need for successful work on project

The problems that I want to solve:

  • Good names of directories so that their purpose is clear.
  • Keeping all project files (including virtualenv) in one place, so I can easily copy, move, archive, remove whole project or estimate disk space usage.
  • Creating multiple copies of some selected file sets such as entire application, repository or virtualenv, while keeping single copy of another files that I don't want to clone.
  • Deploying right set of files to the server simply by rsyncing selected one dir.

解决方案

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

  • Stand-alone websites
  • Pluggable applications

Stand-alone website

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
...

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.

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.

Requirements

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

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

setup.py

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

Project-specific apps

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

Templates/static/locale/tests files

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.

Tmp directory

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.

Virtualenv

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

Project template

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

Deployment

Deployment of this project is following:

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

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

Recently, I made [django-deploy][2] 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.

Sketches and drafts

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.

Pluggable application

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
...

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天全站免登陆