cookiecutter-django是github上有着超过6000多颗星的第三方库,可以快速生成比django-admin startproject命令生成的默认目录更优秀的目录布局。今天我们就来看看cookiecutter-django生成的项目目录结构的优点,讲下为什么它胜出的原因。
我们先看下Django默认项目目录,相信你一定并不陌生。
-
myproject
-
├──
myproject
-
│ ├── __
init__
.py
-
│ ├──
settings
.py
-
│ ├──
urls
.py
-
│ └──
wsgi
.py
-
└──
manage
.py
而利用cookiecutter-django生成的项目目录结构如下所示。它把原myproject目录下的项目设置文件比如settings.py, urls.py和wsgi.py都移到了一个专门的管理设置的config文件夹,这样原myproject目录变成用于存放各个app所属代码,逻辑变得更清晰。
除此以外settings.py文件变成了settings文件夹,分别用于存放开发及生产环境下的配置文件。
-
├──
settings
-
│ ├── __
init__
.py
-
│ ├──
base
.py
-
│ ├──
local
.py
-
│ └──
production
.py
cookiecutter-django借助于django-environ这个第三方库区分不同环境,并把设置文件里的敏感信息放在环境变量里集中管理,而不是代码中,这是非常聪明的做法。一来更安全,二来后续如果需要修改项目配置,只需要修改环境变量即可,不需要修改一行代码。如下所示:
-
from .base import env
-
-
-
# GENERAL
-
# ------------------------------------------------------------------------------
-
# https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
-
SECRET_KEY = env("DJANGO_SECRET_KEY")
-
# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
-
ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default=["xxx.com"])
除此以外,cookiecutter-django将设置文件里的INSTALLED APP分成了3类, Django自带APP, 第三方APP和用户自己开发的APP,便于维护。
-
# APPS
-
# ------------------------------------------------------------------------------
-
DJANGO_APPS = [
-
"django.contrib.auth",
-
"django.contrib.contenttypes",
-
"django.contrib.sessions",
-
"django.contrib.sites",
-
"django.contrib.messages",
-
"django.contrib.staticfiles",
-
"django.contrib.admin",
-
]
-
THIRD_PARTY_APPS = [
-
"allauth",
-
"allauth.account",
-
"allauth.socialaccount",
-
"rest_framework",
-
"django_celery_beat",
-
]
-
-
-
LOCAL_APPS = [
-
"myproject.users.apps.UsersConfig",
-
# Your stuff: custom apps go here
-
]
-
# https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
-
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
另外依赖文件也由一个requirements.txt也变成了一个文件夹,分别存放基础依赖文件base.txt,以及开发和生产环境下分别使用的local.txt和production.txt。
每个requirements.txt也分成了python, django和DRF三类。
-
#python
-
pytz==2019.3
-
python-slugify==4.0.0
-
Pillow==6.2.1
-
redis==3.4.1
-
celery==4.3.0
-
oauthlib==3.1.0
-
python3-openid==3.1.0
-
requests==2.22.0
-
requests-oauthlib==1.3.0
-
six==1.13.0
-
sqlparse==0.3.0
-
urllib3==1.25.7
-
# Django
-
# ------------------------------------------------------------------------------
-
django==3.0 # pyup: < 3.0 # https://www.djangoproject.com/
-
django-allauth==0.41.0 # https://github.com/pennersr/django-allauth
-
django-redis==4.11.0 # https://github.com/niwinz/django-redis
-
django-filter==2.2.0
-
django-celery-beat==1.6.0 # https://github.com/celery/django-celery-beat | Django backed periodic tasks
-
django-crispy-forms==1.7.2 # https://github.com/django-crispy-forms/django-crispy-forms
-
django-environ==0.4.5 # https://github.com/joke2k/django-environ
-
django-model-utils==3.2.0 # https://github.com/jazzband/django-model-utils
-
-
-
-
-
# Django REST Framework
-
djangorestframework==3.10.2 # https://github.com/encode/django-rest-framework
-
coreapi==2.3.3 # https://github.com/core-api/python-client
小结
cookiecutter-django生成的项目模板布局有着明显的优势。哪怕你不使用这个第三方库,你也可以学习它的优点,让你的项目更专业。
转载:https://blog.csdn.net/weixin_42134789/article/details/104289970