项目结构

不同的开发者会根据自己不同经验构造项目

Django默认的目录结构


# 创建一个项目  

django-admin.py startproject mysite
cd mysite
django-admin.py startapp my_app

# 项目结构

mysite/
├── manage.py
├── my_app
   ├── __init__.py
   ├── admin.py
   ├── apps.py
   ├── migrations
      └── __init__.py
   ├── models.py
   ├── tests.py
   └── views.py
└── mysite
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py

一种更好的布局形式

<repository_root>/
├── <configuration_root>/
├── <django_project_root>/

Top Level: Repository Root

需要一个仓库 Repositorygit代码管理

防止丢失

Second Level: Django Project Root

真正项目的文件目录

Second Level: Configuration Root

需要是一个可以导入的 package, 包含__init__

This must be a valid Python package (containing an __init__.py module).


The <configuration_root> directory 需要包含
    
    settings module 
    
    base URLConf (urls.py) 

简单的实例

icecreamratings_project  ===>>  Repository
├── config/
│   ├── settings/
│   ├── __init__.py
│   ├── urls.py
│   └── wsgi.py
├── docs/
├── icecreamratings/
│   ├── media/ # Development only!
│   ├── products/
│   ├── profiles/
│   ├── ratings/
│   ├── static/
│   └── templates/
├── .gitignore
├── Makefile
├── README.rst
├── manage.py
└── requirements.txt

总体项目结构

File or Directory Purpose
.gitignore Lists the files and directories that Git should ignore. (This file is different for other version control systems. For example, if you are using Mercurial instead, you’d have an .hgignore file.)
config/ The of the project, where project-wide settings, urls.py, and wsgi.py modules are placed
Makefile Contains simple deployment tasks and macros. For more complex deployments you may want to rely on tools like Invoke, Paver, or Fabric
manage.py If you leave this in, don’t modify its contents
README.rst and docs/ Developer-facing project documentation.
requirements.txt A list of Python packages required by your project, including the Django 1.11 package.
icecreamratings/ The of the project

实际的项目

File or Directory Purpose
media/ For use in development only: user-generated static media assets such as photos uploaded by users. For larger projects, this will be hosted on separate static media server(s).
products/ App for managing and displaying ice cream brands.
profiles/ App for managing and displaying user profiles.
ratings/ App for managing user ratings.
static/ Non-user-generated static media assets including CSS, JavaScript, and images. For larger projects, this will be hosted on separate static media server(s).
templates/ Where you put your site-wide Django templates

虚拟环境放置

独立于 repository_root, 并列关系

~/projects/icecreamratings_project/

~/.envs/icecreamratings/

自动生成django项目的工具 cookiecutter

cookiecutter

cookiecutter-django-rest

Buy me a 肥仔水!