cookiecutter 构建Django项目初体验

钱远
2023-12-01

INFO

Cookiecutter 是一个通过项目模板创建项目的命令行工具,用于构建Python、Java、JavaScript等项目。 传送门

安装 cookiecutter

pip install cookiecutter

运行指定项目模板并根据提示创建项目

cookiecutter https://github.com/pydanny/cookiecutter-django

执行过程

project_name [My Awesome Project]: sonsuz
project_slug [sonsuz]:
description [Behold My Awesome Project!]:
author_name [Daniel Roy Greenfeld]: jairo
domain_name [example.com]:
email [jairo@example.com]: jairoguo@163.com
version [0.1.0]:
Select open_source_license:
1 - MIT
2 - BSD
3 - GPLv3
4 - Apache Software License 2.0
5 - Not open source
Choose from 1, 2, 3, 4, 5 [1]:
timezone [UTC]: Asia/shanghai
windows [n]: y
use_pycharm [n]: y
use_docker [n]:
Select postgresql_version:
1 - 11.3
2 - 10.8
3 - 9.6
4 - 9.5
5 - 9.4
Choose from 1, 2, 3, 4, 5 [1]:
Select js_task_runner:
1 - None
2 - Gulp
Choose from 1, 2 [1]:
Select cloud_provider:
1 - AWS
2 - GCP
3 - None
Choose from 1, 2, 3 [1]: 3
use_drf [n]:
custom_bootstrap_compilation [n]:
use_compressor [n]: y
use_celery [n]: y
use_mailhog [n]:
use_sentry [n]:
use_whitenoise [n]: y
use_heroku [n]:
Select ci_tool:
1 - None
2 - Travis
3 - Gitlab
Choose from 1, 2, 3 [1]:
keep_local_envs_in_vcs [y]: n
debug [n]: y
 [WARNING]: You chose not to use a cloud provider, media files won't be served in production.
 [SUCCESS]: Project initialized, keep up the good work!

项目结构

F:\PROJECTS\SONSUZ
├─.idea
│  └─runConfigurations
├─config
│  └─settings
├─docs
│  └─pycharm
│      └─images
├─locale
├─requirements
├─sonsuz
│  ├─contrib
│  │  └─sites
│  │      └─migrations
│  ├─static
│  │  ├─css
│  │  ├─fonts
│  │  ├─images
│  │  │  └─favicons
│  │  ├─js
│  │  └─sass
│  ├─templates
│  │  ├─account
│  │  ├─pages
│  │  └─users
│  ├─users
│  │  ├─migrations
│  │  └─tests
│  └─utils
└─utility

完整项目结构及配置

F:\PROJECTS\SONSUZ
│  .coveragerc	#覆盖度测试报告
│  .editorconfig	#编辑器环境配置
│  .gitattributes	#git config
│  .gitignore	#git config
│  .pre-commit-config.yaml
│  .pylintrc	#Pylint规范配置
│  CONTRIBUTORS.txt		#贡献者名单
│  LICENSE		#软件许可证
│  manage.py
│  pytest.ini	#测试配置
│  README.rst	#自诉文件
│  setup.cfg	#启动项配置样版
│
├─.idea
│  │  misc.xml
│  │  modules.xml
│  │  sonsuz.iml
│  │  vcs.xml
│  │  webResources.xml
│  │  workspace.xml
│  │
│  └─runConfigurations
│          merge_production_dotenvs_in_dotenv.xml
│          migrate.xml
│          pytest__users.xml
│          pytest___.xml
│          runserver.xml
│          runserver_plus.xml
│
├─config	#配置
│  │  celery_app.py
│  │  urls.py
│  │  wsgi.py
│  │  __init__.py
│  │
│  └─settings	#Python开发配置
│          base.py
│          local.py
│          production.py
│          test.py
│          __init__.py
│
├─docs		#文档
│  │  conf.py
│  │  index.rst
│  │  make.bat
│  │  Makefile
│  │  __init__.py
│  │
│  └─pycharm
│      │  configuration.rst
│      │
│      └─images
│              1.png
│              2.png
│              3.png
│              4.png
│              7.png
│              8.png
│              f1.png
│              f2.png
│              f3.png
│              f4.png
│              issue1.png
│              issue2.png
│
├─locale	#国际化
│      README.rst
│
├─requirements	#Python包依赖性配置
│      base.txt		#基本配置
│      local.txt	#本地开发
│      production.txt	#生产环境
│
├─sonsuz	#工程目录
│  │  conftest.py
│  │  __init__.py
│  │
│  ├─contrib
│  │  │  __init__.py
│  │  │
│  │  └─sites
│  │      │  __init__.py
│  │      │
│  │      └─migrations
│  │              0001_initial.py
│  │              0002_alter_domain_unique.py
│  │              0003_set_site_domain_and_name.py
│  │              __init__.py
│  │
│  ├─static
│  │  ├─css
│  │  │      project.css
│  │  │
│  │  ├─fonts
│  │  │      .gitkeep
│  │  │
│  │  ├─images
│  │  │  └─favicons
│  │  │          favicon.ico
│  │  │
│  │  ├─js
│  │  │      project.js
│  │  │
│  │  └─sass
│  │          custom_bootstrap_vars.scss
│  │          project.scss
│  │
│  ├─templates
│  │  │  403.html
│  │  │  404.html
│  │  │  500.html
│  │  │  base.html
│  │  │
│  │  ├─account
│  │  │      account_inactive.html
│  │  │      base.html
│  │  │      email.html
│  │  │      email_confirm.html
│  │  │      login.html
│  │  │      logout.html
│  │  │      password_change.html
│  │  │      password_reset.html
│  │  │      password_reset_done.html
│  │  │      password_reset_from_key.html
│  │  │      password_reset_from_key_done.html
│  │  │      password_set.html
│  │  │      signup.html
│  │  │      signup_closed.html
│  │  │      verification_sent.html
│  │  │      verified_email_required.html
│  │  │
│  │  ├─pages
│  │  │      about.html
│  │  │      home.html
│  │  │
│  │  └─users
│  │          user_detail.html
│  │          user_form.html
│  │
│  ├─users
│  │  │  adapters.py
│  │  │  admin.py
│  │  │  apps.py
│  │  │  forms.py
│  │  │  models.py
│  │  │  tasks.py
│  │  │  urls.py
│  │  │  views.py
│  │  │  __init__.py
│  │  │
│  │  ├─migrations
│  │  │      0001_initial.py
│  │  │      __init__.py
│  │  │
│  │  └─tests
│  │          factories.py
│  │          test_forms.py
│  │          test_models.py
│  │          test_tasks.py
│  │          test_urls.py
│  │          test_views.py
│  │          __init__.py
│  │
│  └─utils
│          context_processors.py
│          __init__.py
│
└─utility	#实用工具
        install_os_dependencies.sh
        install_python_dependencies.sh
        requirements-bionic.apt
        requirements-buster.apt
        requirements-jessie.apt
        requirements-stretch.apt
        requirements-trusty.apt
        requirements-xenial.apt

cookiecutter 选项参考

选项功能
project_name项目名称
project_slug项目的slug
description项目描述信息
author_name项目作者
email电子邮件
domain_name计划在项目上线后使用的域名
version项目开始时的版本
open_source_license项目的软件许可证
timezone设置时区
windows是否应为Windows上的开发配置项目
use_pycharm是否应将项目配置为使用PyCharm进行开发
use_docker是否应将项目配置为使用Docker和Docker Compose
postgresql_version选择要使用的PostgreSQL版本
js_task_runner选择一个JavaScript任务运行器
cloud_provider为静态和媒体文件选择云提供商(如果不选择云提供商,则媒体文件将无法工作。)
mail_service选择Django-Anymail提供的电子邮件服务
use_drf是否应将项目配置为使用Django Rest Framework
custom_bootstrap_compilation项目是否应通过选定的JavaScript任务运行程序的任务支持Bootstrap重新编译
use_compressor是否应将项目配置为使用Django Compressor(js和css压缩处理)
use_celery是否应将项目配置为使用Celery(分布式任务队列)
use_mailhog是否应将项目配置为使用MailHog(电子邮件测试工具)
use_sentry是否应将项目配置为使用Sentry(实时错误追踪系统)
use_whitenoise是否应将项目配置为使用WhiteNoise(静态文件服务)
use_heroku是否应配置项目以使其可部署到Heroku(云服务平台)
ci_tool选择配置项工具以运行测试
keep_local_envs_in_vcs是否应将项目的.envs /.local/保留在VCS中
debug指示是否应配置项目以进行调试

注:cloud_provider 选择为 None 将无法创建项目,此时必须将 use_whitenoise设置为 y

 类似资料: