【Django开发】0到1美多商城项目md教程第1篇:欢迎来到美多商城!【附代码
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133145975-1222707960.png本系列文章md笔记(已分享)主要讨论django商城项目相关知识。项目利用Django框架开发一套前后端不分离的商城项目(4.0版本)含代码和文档。功能包括前后端不分离,方便SEO。采用Django + Jinja2模板引擎 + Vue.js实现前后端逻辑,Nginx服务器(反向代理)Nginx服务器(静态首页、商品详情页、uwsgi服务器(美多阛阓业务场景),后端服务:MySQL、Redis、Celery、RabbitMQ、Docker、FastDFS、Elasticsearch、Crontab,外部接口:容联云、互联、。
全套笔记资料代码移步: 前往gitee仓库查看
感兴趣的小伙伴可以自取哦,欢迎大家点赞转发~
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133146296-2036026748.png
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133146524-493999179.png
欢迎来到美多商城!
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133146768-1240147072.png
项目准备
项目介绍
项目需求分析
需求分析缘故原由:
[*]可以团体的了解项目的业务流程和主要的业务需求。
[*]项目中,需求驱动开发。即开发人员须要以需求为目的来实现业务逻辑。
需求分析方式:
[*]企业中,借助 产物原型图 分析需求。
[*]需求分析完后,前端按照产物原型图开发前端页面,后端开发对应的业务及相应处理。
需求分析内容:
[*]页面及其业务流程和业务逻辑。
提示:
[*]我们如今借助 示例网站 作为原型图来分析需求。
1. 项目主要页面介绍
1.首页广告
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133147128-1230267439.png
2.注册
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133147424-2073780229.png
3.登录
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133147674-1321278118.png
4.登录
https://www.cnblogs.com/images/03%E7%99%BB%E5%BD%95.png
https://www.cnblogs.com/images/03%E7%94%A8%E6%88%B7%E7%BB%91%E5%AE%9A.png
5.个人信息
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133147950-1283038188.png
6.收货地址
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133148303-1214160437.png
7.我的订单
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133148517-1798346747.png
8.修改密码
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133148737-289388521.png
9.商品列表
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133148912-1900794324.png
10.商品搜索
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133149165-447673708.png
11.商品详情
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133149429-173181203.png
12.购物车
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133149704-1954523047.png
13.结算订单
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133149928-2100287805.png
14.提交订单
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133150184-1344576601.png
15.支付
https://www.cnblogs.com/images/15%E6%94%AF%E4%BB%98.png
16.支付结果处理
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133150389-383411343.png
17.订单商品评价
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133150567-1635722928.png
2. 归纳项目主要模块
为了方便项目管理及多人协同开发,我们根据需求将功能划分为不同的模块。
将来在项目中,每个模块都会对应一个子应用进行管理和解耦。
模块功能验证图形验证、短信验证用户注册、登录、用户中心第三方登录登录首页广告首页广告商品商品列表、商品搜索、商品详情购物车购物车管理、购物车合并订单确认订单、提交订单支付支付、订单商品评价MIS系统数据统计、用户管理、权限管理、商品管理、订单管理3. 知识要点
[*]需求分析缘故原由:需求驱动开发。
[*]需求分析方式:企业中,使用产物原型图。
[*]需求分析内容:页面及业务逻辑。
[*]需求分析结果:划分业务模块,明确每个模块下的主要功能,并以子应用的形式进行管理。
项目架构计划
1. 项目开发模式
选项技能选型开发模式前后端不分离后端框架Django + Jinja2模板引擎前端框架Vue.js阐明:
[*]前后端不分离的开发模式,是为了提高搜索引擎排名,即SEO。特别是首页,详情页和列表页。
[*]页面须要团体刷新:我们会选择使用Jinja2模板引擎来实现。
[*]页面须要局部刷新:我们会选择使用Vue.js来实现。
2. 项目运行机制
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133150787-680365496.png
3. 知识要点
[*]项目开发模式
[*]前后端不分离,方便SEO。
[*]采用Django + Jinja2模板引擎 + Vue.js实现前后端逻辑。
[*]项目运行机制
[*]代理服务:Nginx服务器(反向代理)
[*]静态服务:Nginx服务器(静态首页、商品详情页、...)
[*]动态服务:uwsgi服务器(美多阛阓业务场景)
[*]后端服务:MySQL、Redis、Celery、RabbitMQ、Docker、FastDFS、Elasticsearch、Crontab
[*]外部接口:容联云、互联、
工程创建和配置
创建工程
美多商城项目源代码采用长途仓库托管。
1. 准备项目代码仓库
1.源码托管网站
[*]码云([
2.创建源码长途仓库:meiduo_project
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133151024-686703597.png
2. 克隆项目代码仓库
1.进入当地项目目次
$ mkdir ~/projects
$ cd projects/2.克隆仓库
$ git clone 3. 创建美多商城工程
1.进入当地项目仓库
$ cd ~/projects/meiduo_project/2.创建美多商城假造情况,安装Django框架
$ mkvirtualenv -p python3 meiduo_mall
$ pip install django==1.11.113.创建美多商城Django工程
$ django-admin startproject meiduo_mall创建工程完成后:运行程序,测试结果。
配置开发情况
美多商城项目的情况分为开发情况和生产情况。
[*]开发情况:用于编写和调试项目代码。
[*]生产情况:用于项目线上部署运行。
1. 新建配置文件
[*]准备配置文件目次
[*]新建包,命名为settings,作为配置文件目次
[*]准备开发和生产情况配置文件
[*]在配置包settings中,新建开发和生产情况配置文件
[*]准备开发情况配置内容
[*]将默认的配置文件settings.py中内容拷贝至dev.py
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133151191-53940678.png
2. 指定开发情况配置文件
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133151405-1917682369.png
配置完成后:运行程序,测试结果。
配置Jinja2模板引擎
美多商城的模板采用Jinja2模板引擎。
1. 安装Jinja2扩展包
$ pip install Jinja22. 配置Jinja2模板引擎
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',# jinja2模板引擎
'DIRS': ,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]3. 增补Jinja2模板引擎情况
1.创建Jinja2模板引擎情况配置文件
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133151644-1039350553.png
2.编写Jinja2模板引擎情况配置代码
from jinja2 import Environment
from django.contrib.staticfiles.storage import staticfiles_storage
from django.urls import reverse
def jinja2_environment(**options):
env = Environment(**options)
env.globals.update({
'static': staticfiles_storage.url,
'url': reverse,
})
return env
"""
确保可以使用模板引擎中的{{ url('') }} {{ static('') }}这类语句
"""https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133151798-18823758.png
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133151963-1243557851.png
3.加载Jinja2模板引擎情况
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',# jinja2模板引擎
'DIRS': ,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
# 补充Jinja2模板引擎环境
'environment': 'meiduo_mall.utils.jinja2_env.jinja2_environment',
},
},
]配置完成后:运行程序,测试结果。
配置MySQL数据库
美多商城数据存储服务采用MySQL数据库。
1. 新建MySQL数据库
1.新建MySQL数据库:meiduo_mall
$ create database meiduo charset=utf8;2.新建MySQL用户
$ create user itheima identified by '123456';3.授权itcast用户访问meiduo_mall数据库
$ grant all on meiduo.* to 'itheima'@'%';4.授权竣过后刷新特权
$ flush privileges;2. 配置MySQL数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # 数据库引擎
'HOST': '127.0.0.1', # 数据库主机
'PORT': 3306, # 数据库端口
'USER': 'itheima', # 数据库用户名
'PASSWORD': '123456', # 数据库用户密码
'NAME': 'meiduo' # 数据库名字
},
}大概出现的错误
[*]Error loading MySQLdb module: No module named 'MySQLdb'.
出现错误的缘故原由:
[*]Django中操作MySQL数据库须要驱动程序MySQLdb
[*]目前项目假造情况中没有驱动程序MySQLdb
解决办法:
[*]安装PyMySQL扩展包
[*]因为MySQLdb只实用于Python2.x的版本,Python3.x的版本中使用PyMySQL替代MySQLdb
3. 安装PyMySQL扩展包
1.安装驱动程序
$ pip install PyMySQL2.在工程同名子目次的__init__.py文件中,添加如下代码:
from pymysql import install_as_MySQLdb
install_as_MySQLdb()配置完成后:运行程序,测试结果。
配置Redis数据库
美多商城数据缓存服务采用Redis数据库。
1. 安装django-redis扩展包
1.安装django-redis扩展包
$ pip install django-redis2.django-redis使用阐明文档
[点击进入文档](
2. 配置Redis数据库
CACHES = {
"default": { # 默认
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
"session": { # session
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
}
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "session"default:
[*]默认的Redis配置项,采用0号Redis库。
session:
[*]状态保持的Redis配置项,采用1号Redis库。
SESSION_ENGINE
[*]修改session存储机制使用Redis保存。
SESSION_CACHE_ALIAS:
[*]使用名为"session"的Redis配置项存储session数据。
配置完成后:运行程序,测试结果。
配置工程日志
美多商城的日志记载采用logging模块。
1. 配置工程日志
LOGGING = {
'version': 1,
'disable_existing_loggers': False,# 是否禁用已经存在的日志器
'formatters': {# 日志信息显示的格式
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(lineno)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(module)s %(lineno)d %(message)s'
},
},
'filters': {# 对日志进行过滤
'require_debug_true': {# django在debug模式下才输出日志
'()': 'django.utils.log.RequireDebugTrue',
},
},
'handlers': {# 日志处理方法
'console': {# 向终端中输出日志
'level': 'INFO',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'file': {# 向文件中输出日志
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(os.path.dirname(BASE_DIR), 'logs/meiduo.log'),# 日志文件的位置
'maxBytes': 300 * 1024 * 1024,
'backupCount': 10,
'formatter': 'verbose'
},
},
'loggers': {# 日志器
'django': {# 定义了一个名为django的日志器
'handlers': ['console', 'file'],# 可以同时向终端与文件中输出日志
'propagate': True,# 是否继续传递日志信息
'level': 'INFO',# 日志器接收的最低日志级别
},
}
}2. 准备日志文件目次
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133152119-448691151.png
3. 日志记载器的使用
import logging
# 创建日志记录器
logger = logging.getLogger('django')
# 输出日志
logger.debug('测试logging模块debug')
logger.info('测试logging模块info')
logger.error('测试logging模块error')4. Git管理工程日志
提示1:
[*]开发过程中,产生的日志信息不须要代码仓库进行管理和记载。
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133152291-2014114551.png
提示2:
[*]建立代码仓库时,生成的忽略文件中已经默认忽略掉了 *.log 。
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133152491-54197044.png
题目:
[*]logs文件目次需求被Git仓库记载和管理。
[*]当把 *.log 都忽略掉后,logs文件目次为空。
[*]但是,Git是不允许提交一个空的目次到版本库上的。
解决:
[*]在空文件目次中建立一个 .gitkeep 文件,然后即可提交。
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133152652-1943046494.png
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133152825-554267201.png
配置完成后:运行程序,测试结果。
5. 知识要点
[*]本项目最低日志品级设置为:INFO
[*]创建日志记载器的方式:
logger = logging.getLogger('django')
[*]日志记载器的使用:
logger.info('测试logging模块info')
[*]在日志loggers选项中可以指定多个日志记载器
配置前端静态文件
美多商城项目中须要使用静态文件,好比 css、images、js 等等。
1. 准备静态文件
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133153014-1211104560.png
https://img2024.cnblogs.com/other/3367718/202403/3367718-20240310133153185-249586918.png
2. 指定静态文件加载路径
STATIC_URL = '/static/'
# 配置静态文件加载路径
STATICFILES_DIRS = 配置完成后:运行程序,测试结果。
[*][
用户注册
展示用户注册页面
未完待续, 同砚们请等待下一期
全套笔记资料代码移步: 前往gitee仓库查看
感兴趣的小伙伴可以自取哦,欢迎大家点赞转发~
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]