Django Salesman 项目教程
1. 项目的目录结构及介绍
Django Salesman 项目的目录结构如下:
django-salesman/
├── docs/
├── example/
├── salesman/
│ ├── basket/
│ ├── checkout/
│ ├── orders/
│ ├── payment/
│ ├── utils/
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── urls.py
│ └── views.py
├── .coveragerc
├── .flake8
├── .gitignore
├── .pre-commit-config.yaml
├── .readthedocs.yaml
├── CODE_OF_CONDUCT.md
├── LICENSE
├── README.md
├── SECURITY.md
├── pyproject.toml
└── pytest.ini
目录介绍
docs/
: 项目文档目录。example/
: 示例应用目录。salesman/
: 核心应用目录,包含购物篮、结账、订单和支付等模块。.coveragerc
: 代码覆盖率配置文件。.flake8
: Flake8 代码风格检查配置文件。.gitignore
: Git 忽略文件配置。.pre-commit-config.yaml
: 预提交钩子配置文件。.readthedocs.yaml
: Read the Docs 配置文件。CODE_OF_CONDUCT.md
: 行为准则文件。LICENSE
: 项目许可证文件。README.md
: 项目说明文件。SECURITY.md
: 安全相关说明文件。pyproject.toml
: Python 项目配置文件。pytest.ini
: Pytest 配置文件。
2. 项目的启动文件介绍
Django Salesman 项目的启动文件主要是 manage.py
,通常位于项目根目录下。这个文件用于启动 Django 开发服务器、运行管理命令等。
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'example.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
启动命令
python manage.py runserver
3. 项目的配置文件介绍
Django Salesman 项目的配置文件主要是 settings.py
,通常位于 example/
目录下。这个文件包含了 Django 项目的所有配置。
"""
Django settings for example project.
Generated by 'django-admin startproject' using Django 3.2.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'your-secret-key'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'salesman',