项目需求:

网站后台管理、前台大堂点餐和移动端会员点餐三部分:
image.png
image.pngimage.png
image.png

数据库设计

image.png
bb1.png

项目架构的程序设计

  • 基于Python语言,版本:>=3.6及以上。
  • 使用Django框架,版本:2.2.*的LTS版本。
  • MySQL数据库
  • 连接数据库:mysqlclient=1.4.6
  • 图像处理: Pillow=5.0.0
  • Web前端技术:HTML、CSS、JavaScript和Jquery等

项目共计三个应用:myadmin、web和mobile

  1. myobject/ 项目目录
  2. |
  3. |--myadmin/ 后台管理应用
  4. | |--__init__.py
  5. | |--views/
  6. | | |--index.py 后台首页、登录、退出、验证码加载等视图方法
  7. | | |--user.py 员工信息管理视图
  8. | | |--member.py 会员信息管理视图
  9. | | |--shop.py 店铺信息管理视图
  10. | | |--product.py 菜品信息管理视图
  11. | | |--orders.py 订单信息管理视图
  12. | | |--.....
  13. | |
  14. | |-- admin.py
  15. | |-- apps.py
  16. | |-- models.py 定义了整个网站的models类,除了本应使用,还提供给其他应用
  17. | |-- shopmiddleware.py 定了整个网站的中间件(验证是否登录及权限管理信息)
  18. | |-- tests.py
  19. | |-- urls.py 配置了整个网站后台所有请求路由
  20. |
  21. |--web/ 前台应用(大堂点餐)
  22. | |-- __init__.py
  23. | |--views/
  24. | | |--index.py 大堂点餐应用的登录、退出、验证码、加载店铺等方法
  25. | | |--product.py 菜品展示视图
  26. | | |--shopcart.py 购物车管理视图
  27. | | |--orders.py 订单管理视图
  28. | |-- admin.py
  29. | |-- apps.py
  30. | |-- models.py
  31. | |-- tests.py
  32. | |-- urls.py 配置了大堂点餐应用的所有请求路由
  33. |
  34. |--mobile/ 移动端点餐应用\
  35. | |-- __init__.py
  36. | |--views/
  37. | | |--index.py 登录,退出路由
  38. | | |--shop.py 店铺信息加载视图
  39. | | |--product.py 菜品信息加载展示视图
  40. | | |--orders.py 个人订单信息管理视图
  41. | | |--member.py 个人中心管理视图
  42. | | |--......
  43. | |
  44. | |-- admin.py
  45. | |-- apps.py
  46. | |-- models.py
  47. | |-- tests.py
  48. | |-- urls.py 配置了移动端点餐应用的所有请求路由
  49. |
  50. |--myobject/ 项目目录
  51. | |-- __init__.py
  52. | |-- settings.py
  53. | |-- urls.py
  54. | |-- wsgi.py
  55. |
  56. |--static/ 静态资源目录
  57. | |-- uploads/ 上传文件存储目录
  58. | |-- myadmin/ 后台管理静态资源目录
  59. | |-- web/ 大堂点餐静态资源目录
  60. | |-- mobile/ 移动端管理静态资源目录
  61. |
  62. |--templates/ 模板目录
  63. | |-- myadmin/ 后台管理模板目录
  64. | |-- web/ 大堂点餐模板目录
  65. | |-- mobile/ 移动端管理模板目录
  66. |
  67. |--manage.py 入口文件

访问url设计:

  1. http://主机名:端口/应用名/视图名/函数名
  1. 视图中的函数命名格式:
  2. index() ---- 浏览信息
  3. add() ---- 加载添加界面
  4. insert() ---- 执行添加
  5. delete() ---- 执行删除(路由中使用del)
  6. edit() ---- 加载编辑界面
  7. update() ---- 执行信息编辑

项目搭建

  1. # 创建项目框架 `myobject`
  2. django-admin startproject myobject
  3. cd myobject
  4. # 在项目中创建一个myadmin应用(项目的后台管理)
  5. python manage.py startapp myadmin
  6. # 在项目中再创建一个web应用(项目前台大堂点餐应用)
  7. python manage.py startapp web
  8. # 在项目中再创建一个mobile应用(移动客户点餐端应用)
  9. python manage.py startapp mobile
  10. # 创建模板目录
  11. mkdir templates
  12. mkdir templates/myadmin
  13. mkdir templates/web
  14. mkdir templates/mobile
  15. # 创建静态资源目录
  16. mkdir static
  17. mkdir static/myadmin
  18. mkdir static/web
  19. mkdir static/mobile
  20. mkdir static/uploads
  21. # 创建前后台应用模板目录,并在里面各创建一个`__init__.py`和`index.py`的空文件
  22. mkdir myadmin/views
  23. touch myadmin/views/__init__.py
  24. touch myadmin/views/index.py
  25. mkdir web/views
  26. touch web/views/__init__.py
  27. touch web/views/index.py
  28. mkdir mobile/views
  29. touch mobile/views/__init__.py
  30. touch mobile/views/index.py
  31. # 删除前后台应用的默认模板文件
  32. # rm -rf myadmin/views.py
  33. # rm -rf web/views.py
  34. # rm -rf mobile/views.py
  35. # 拷贝路由文件到应用目录中
  36. cp myobject/urls.py myadmin/urls.py
  37. cp myobject/urls.py web/urls.py
  38. cp myobject/urls.py mobile/urls.py
  39. # 退出项目目录
  40. cd ..
  41. #查看项目目录结构
  42. tree myobject
  43. myobject/
  44. ├── manage.py
  45. ├── myobject
  46. ├── __init__.py
  47. ├── settings.py
  48. ├── urls.py
  49. └── wsgi.py
  50. ├── myadmin
  51. ├── admin.py
  52. ├── apps.py
  53. ├── __init__.py
  54. ├── migrations
  55. └── __init__.py
  56. ├── views
  57. ├── __init__.py
  58. └── index.py
  59. ├── models.py
  60. ├── tests.py
  61. └── urls.py
  62. ├── web
  63. ├── admin.py
  64. ├── apps.py
  65. ├── __init__.py
  66. ├── migrations
  67. ├── __init__.py
  68. ├── views
  69. ├── __init__.py
  70. └── index.py
  71. ├── models.py
  72. ├── tests.py
  73. └── urls.py
  74. ├── mobile
  75. ├── admin.py
  76. ├── apps.py
  77. ├── __init__.py
  78. ├── migrations
  79. ├── __init__.py
  80. ├── views
  81. ├── __init__.py
  82. └── index.py
  83. ├── models.py
  84. ├── tests.py
  85. └── urls.py
  86. ├── static
  87. ├── myadmin/
  88. ├── web/
  89. ├── mobile/
  90. └── uploads/
  91. └── templates
  92. ├── myadmin/
  93. ├── web/
  94. └── mobile/

配置:
注意:此配置需要安装mysql数据库mysqlclient软件包, 如:$ pip install mysqlclient
创建数据库 shop

  1. -- 员工信息表
  2. CREATE TABLE `user` (
  3. `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '员工账号id',
  4. `username` varchar(50) DEFAULT NULL COMMENT '员工账号',
  5. `nickname` varchar(50) DEFAULT NULL COMMENT '昵称',
  6. `password_hash` varchar(100) DEFAULT NULL COMMENT '密码',
  7. `password_salt` varchar(50) DEFAULT NULL COMMENT '密码干扰值',
  8. `status` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '状态:1正常/2禁用/9删除',
  9. `create_at` datetime DEFAULT NULL COMMENT '创建时间',
  10. `update_at` datetime DEFAULT NULL COMMENT '修改时间',
  11. PRIMARY KEY (`id`)
  12. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
  13. -- 店铺信息表
  14. CREATE TABLE `shop` (
  15. `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '店铺id',
  16. `name` varchar(255) NOT NULL COMMENT '店铺名称',
  17. `cover_pic` varchar(255) DEFAULT NULL COMMENT '封面图片',
  18. `banner_pic` varchar(255) DEFAULT NULL COMMENT '图标Logo',
  19. `address` varchar(255) DEFAULT NULL COMMENT '店铺地址',
  20. `phone` varchar(255) DEFAULT NULL COMMENT '联系电话',
  21. `status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '状态:1:正常 2:暂停 9:删除',
  22. `create_at` datetime DEFAULT NULL COMMENT '添加时间',
  23. `update_at` datetime DEFAULT NULL COMMENT '修改时间',
  24. PRIMARY KEY (`id`)
  25. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
  26. -- 菜品类别表
  27. CREATE TABLE `category` (
  28. `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '菜品分类id',
  29. `shop_id` int(11) DEFAULT NULL COMMENT '店铺id',
  30. `name` varchar(50) DEFAULT NULL COMMENT '分类名称',
  31. `status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '状态:1正常 9删除',
  32. `create_at` datetime DEFAULT NULL COMMENT '添加时间',
  33. `update_at` datetime DEFAULT NULL COMMENT '修改时间',
  34. PRIMARY KEY (`id`)
  35. ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
  36. -- 菜品信息表
  37. CREATE TABLE `product` (
  38. `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '菜品id',
  39. `shop_id` int(11) DEFAULT NULL COMMENT '店铺id',
  40. `category_id` int(11) DEFAULT NULL COMMENT '菜品分类id',
  41. `cover_pic` varchar(50) DEFAULT NULL COMMENT '菜品图片',
  42. `name` varchar(50) DEFAULT NULL COMMENT '菜品名称',
  43. `price` double(6,2) DEFAULT NULL COMMENT '单价',
  44. `status` tinyint(4) DEFAULT NULL COMMENT '状态:1:正常 2:停售 9:删除',
  45. `create_at` datetime DEFAULT NULL COMMENT '添加时间',
  46. `update_at` datetime DEFAULT NULL COMMENT '修改时间',
  47. PRIMARY KEY (`id`)
  48. ) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
  49. -- 会员信息表
  50. CREATE TABLE `member` (
  51. `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '会员表id',
  52. `nickname` varchar(50) DEFAULT NULL COMMENT '昵称',
  53. `avatar` varchar(255) DEFAULT NULL COMMENT '头像',
  54. `mobile` varchar(50) DEFAULT NULL COMMENT '电话',
  55. `status` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '状态:1正常/2禁用/9删除',
  56. `create_at` datetime DEFAULT NULL COMMENT '添加时间',
  57. `update_at` datetime DEFAULT NULL COMMENT '修改时间',
  58. PRIMARY KEY (`id`)
  59. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
  60. -- 订单信息表
  61. CREATE TABLE `orders` (
  62. `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '订单表id',
  63. `shop_id` int(10) unsigned DEFAULT NULL COMMENT '店铺id号',
  64. `member_id` int(10) unsigned DEFAULT NULL COMMENT '会员id',
  65. `user_id` int(10) unsigned DEFAULT NULL COMMENT '操作员id',
  66. `money` double(8,2) DEFAULT NULL COMMENT '金额',
  67. `status` tinyint(3) unsigned DEFAULT NULL COMMENT '订单状态:1过行中/2无效/3已完成',
  68. `payment_status` tinyint(3) unsigned DEFAULT NULL COMMENT '支付状态:1未支付/2已支付/3已退款',
  69. `create_at` datetime DEFAULT NULL COMMENT '添加时间',
  70. `update_at` datetime DEFAULT NULL COMMENT '修改时间',
  71. PRIMARY KEY (`id`)
  72. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  73. -- 订单信息详情表
  74. CREATE TABLE `order_detail` (
  75. `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '订单详情id',
  76. `order_id` int(10) unsigned DEFAULT NULL COMMENT '订单id',
  77. `product_id` int(10) unsigned DEFAULT NULL COMMENT '菜品id',
  78. `product_name` varchar(50) DEFAULT NULL COMMENT '菜品名称',
  79. `price` double(6,2) unsigned DEFAULT NULL COMMENT '单价',
  80. `quantity` int(10) unsigned NOT NULL DEFAULT '1' COMMENT '数量',
  81. `status` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '状态:1正常/9删除',
  82. PRIMARY KEY (`id`)
  83. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='订单详情信息表';
  84. -- 支付信息表
  85. CREATE TABLE `payment` (
  86. `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '支付表id',
  87. `order_id` int(10) unsigned DEFAULT NULL COMMENT '订单id',
  88. `member_id` int(10) unsigned DEFAULT NULL COMMENT '会员id',
  89. `money` double(8,2) unsigned DEFAULT NULL COMMENT '支付金额',
  90. `type` tinyint(3) unsigned DEFAULT NULL COMMENT '付款方式:1会员付款/2收银收款',
  91. `bank` tinyint(3) unsigned DEFAULT NULL COMMENT '收款银行渠道:1微信/2余额/3现金/4支付宝',
  92. `status` tinyint(3) unsigned DEFAULT NULL COMMENT '支付状态:1未支付/2已支付/3已退款',
  93. `create_at` datetime DEFAULT NULL COMMENT '添加时间',
  94. `update_at` datetime DEFAULT NULL COMMENT '修改时间',
  95. PRIMARY KEY (`id`)
  96. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
  97. CREATE TABLE `django_session` (
  98. `session_key` varchar(40) NOT NULL,
  99. `session_data` longtext NOT NULL,
  100. `expire_date` datetime(6) NOT NULL,
  101. PRIMARY KEY (`session_key`),
  102. KEY `django_session_expire_date_a5c62663` (`expire_date`)
  103. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  104. -- user上表中添加一条后台管理员账户数据
  105. insert into `user`(`id`,`username`,`nickname`,`password_hash`,`password_salt`,`status`,`create_at`,`update_at`) values (1,'zhangsan','张三','1e191d851b3b49a248f4ea62f6b06410','123456',1,'2018-08-08 18:18:18','2018-09-07 08:06:55');

编辑myobject/myobject/init.py文件

  1. #注册数据库
  2. import pymysql
  3. pymysql.install_as_MySQLdb()

编辑myobject/myobject/settings.py文件

  1. # myobject/myobject/settings.py 项目配置文件
  2. # 1. 配置允许访问的主机名信息
  3. ALLOWED_HOSTS = ['*']
  4. ALLOWED_HOSTS = ['localhost','127.0.0.1','192.168.2.240']
  5. ...
  6. # 2. 将myadmin和web的应用添加到项目框架结构中
  7. INSTALLED_APPS = [
  8. 'django.contrib.admin',
  9. 'django.contrib.auth',
  10. 'django.contrib.contenttypes',
  11. 'django.contrib.sessions',
  12. 'django.contrib.messages',
  13. 'django.contrib.staticfiles',
  14. 'myadmin',
  15. 'web',
  16. 'mobile',
  17. ]
  18. ...
  19. # 3. 配置模板目录 os.path.join(BASE_DIR,'templates')
  20. TEMPLATES = [
  21. {
  22. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  23. 'DIRS': [os.path.join(BASE_DIR,'templates')],
  24. 'APP_DIRS': True,
  25. 'OPTIONS': {
  26. 'context_processors': [
  27. 'django.template.context_processors.debug',
  28. 'django.template.context_processors.request',
  29. 'django.contrib.auth.context_processors.auth',
  30. 'django.contrib.messages.context_processors.messages',
  31. ],
  32. },
  33. },
  34. ]
  35. ...
  36. # 4. 配置项目的数据库连接信息:
  37. DATABASES = {
  38. 'default': {
  39. 'ENGINE': 'django.db.backends.mysql',
  40. 'NAME': 'shop',
  41. 'USER': 'root',
  42. 'PASSWORD': '',
  43. 'HOST': 'localhost',
  44. 'PORT': '3306',
  45. }
  46. }
  47. ...
  48. # 5. 设置时区和语言
  49. LANGUAGE_CODE = 'zh-hans'
  50. TIME_ZONE = 'Asia/Shanghai'
  51. ...
  52. # 6. 配置网站的静态资源目录
  53. STATIC_URL = '/static/'
  54. STATICFILES_DIRS = [
  55. os.path.join(BASE_DIR, 'static'),
  56. ]

项目urls路由信息配置:
打开根路由文件:myobject/myobject/urls.py路由文件,编写路由配置信息

  1. # myobject/myobject/urls.py
  2. #from django.contrib import admin
  3. from django.urls import include,path
  4. urlpatterns = [
  5. #path('admin/', admin.site.urls),
  6. path('', include("web.urls")), # 默认前台大堂点餐端
  7. path('myadmin/', include("myadmin.urls")), # 后台管理端
  8. path('mobile/', include("mobile.urls")), # 移动会员端
  9. ]

打开项目后台管理路由文件:myobject/myadmin/urls.py路由文件,编写路由配置信息

  1. # myobject/myadmin/urls.py
  2. from django.urls import path
  3. from myadmin.views import index
  4. urlpatterns = [
  5. # 后台首页
  6. path('', index.index, name="myadmin_index"),
  7. ]

打开项目前台大堂点餐端路由文件:myobject/web/urls.py路由文件,编写路由配置信息

  1. # myobject/web/urls.py
  2. from django.urls import path
  3. from web.views import index
  4. urlpatterns = [
  5. # path('', index.index, name="index"),
  6. ]

打开项目移动会员端路由文件:myobject/mobile/urls.py路由文件,编写路由配置信息

  1. # myobject/mobile/urls.py
  2. from django.urls import path
  3. from web.views import index
  4. urlpatterns = [
  5. # path('', index.index, name="index"),
  6. ]

编写视图测试

  1. # myobject/myadmin/views/index.py
  2. from django.shortcuts import render
  3. from django.http import HttpResponse
  4. #后台首页
  5. def index(request):
  6. return HttpResponse('欢迎进入点餐系统网站后台管理!')
  7. # myobject/web/views/index.py
  8. from django.shortcuts import render
  9. from django.http import HttpResponse
  10. #前台首页
  11. def index(request):
  12. return HttpResponse('欢迎进入大堂点餐前台首页!')
  13. # myobject/mobile/views/index.py
  14. from django.shortcuts import render
  15. from django.http import HttpResponse
  16. #移动端首页
  17. def index(request):
  18. return HttpResponse('欢迎进入移动会员端首页!')

导入页面模板:

  • 从github上下载一个后台简洁模板:https://github.com/ColorlibHQ/AdminLTE
  • 将后台模板目录中的资源目录:bower_components、dist、local、package.json复制到项目的后台静态资源目录static/myadmin/中

    权限设计

    用户登录信息存储在Django框架中的session,而session信息又存放的数据库中,所以要先使用数据迁移命令在MySQL数据库中先生成一些Django默认自带表。 ```plsql python manage.py migrate

    或 执行下面sql语句在shop数据库中创建session表

CREATE TABLE django_session (
session_key varchar(40) NOT NULL, session_data longtext NOT NULL, expire_date datetime(6) NOT NULL, PRIMARY KEY (session_key), KEY django_session_expire_date_a5c62663 (expire_date) ) ENGINE=InnoDB DEFAULT CHARSET=utf8

  1. 通过中间件进行登录权限判断<br />在myadmin应用中创建中间件:
  2. ```plsql
  3. #创建文件: myobject/myadmin/shopmiddleware.py
  4. # 自定义中间件类
  5. from django.shortcuts import redirect
  6. from django.urls import reverse
  7. import re
  8. class ShopMiddleware(object):
  9. def __init__(self, get_response):
  10. self.get_response = get_response
  11. # One-time configuration and initialization.
  12. print("ShopMiddleware")
  13. def __call__(self, request):
  14. # 获取当前请求路径
  15. path = request.path
  16. #print("mycall..."+path)
  17. # 后台请求路由判断
  18. # 定义网站后台不用登录也可访问的路由url
  19. urllist = ['/myadmin/login','/myadmin/dologin','/myadmin/logout','/myadmin/verify']
  20. # 判断当前请求是否是访问网站后台,并且path不在urllist中
  21. if re.match(r"^/myadmin",path) and (path not in urllist):
  22. # 判断当前用户是否没有登录
  23. if "adminuser" not in request.session:
  24. # 执行登录界面跳转
  25. return redirect(reverse('myadmin_login'))
  26. # 请求继续执行下去
  27. response = self.get_response(request)
  28. # Code to be executed for each request/response after
  29. # the view is called.
  30. return response

将自定义的中间件注册到项目中:

  1. #编辑 myobject/settings.py 配置文件
  2. MIDDLEWARE = [
  3. 'django.middleware.security.SecurityMiddleware',
  4. 'django.contrib.sessions.middleware.SessionMiddleware',
  5. 'django.middleware.common.CommonMiddleware',
  6. 'django.middleware.csrf.CsrfViewMiddleware',
  7. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  8. 'django.contrib.messages.middleware.MessageMiddleware',
  9. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  10. 'myadmin.shopmiddleware.ShopMiddleware', #注册中间件
  11. ]

操作界面:
image.pngimage.pngimage.png

代码位置