订单交易平台准备工作
1.先搭建环境
# 1.先创建python基本环境,并且创建虚拟环境# 2.创建完成后,先安装你所需要的Django版本:
pip install Django==3.2# 3.创建Django项目:
django-admin startproject app01 .# 4.创建Django,在app01根目录文件下创建apps文件,里面放app文件:
python manage.py startapp api apps/api
2.环境搭建完成后,进行settings项目配置(Mysql、redis、templates、static)
2.1 在app中创建static文件(css、js、images、plugins)、templates文件
2.2 配置settings文件
# mysql配置
DATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','NAME': 'day06','USER': 'root','PASSWORD': 'zt7758521','HOST': '127.0.0.1','PORT': 3306,}
}# 配置静态文件
STATIC_URL = '/static/'# 配置models文件
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'# 配置将来登录界面所需要的东西
MENU = {"ADMIN": [],"CUSTOMER": [],
}
PERMISSION = {"ADMIN": {},"CUSTOMER": {},
}# 配置cache缓存(redis)
CACHES = {"default": {"BACKEND": "django_redis.cache.RedisCache","LOCATION": "redis://127.0.0.1:6379","OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient","CONNECTION_POOL_KWARGS": {"max_connections": 100},"PASSWORD": "",}}
}# 配置session配置
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
SESSION_CACHE_ALIAS = 'default'SESSION_COOKIE_NAME = "sid" # Session的cookie保存在浏览器上时的key,即:sessionid=随机字符串
SESSION_COOKIE_PATH = "/" # Session的cookie保存的路径
SESSION_COOKIE_DOMAIN = None # Session的cookie保存的域名
SESSION_COOKIE_SECURE = False # 是否Https传输cookie
SESSION_COOKIE_HTTPONLY = True # 是否Session的cookie只支持http传输
SESSION_COOKIE_AGE = 1209600 # Session的cookie失效日期(2周)SESSION_EXPIRE_AT_BROWSER_CLOSE = False # 是否关闭浏览器使得Session过期
SESSION_SAVE_EVERY_REQUEST = True # 是否每次请求都保存Session,默认修改之后才保存# 配置本地local_setting
try:from .local_settings import *
except Exception:pass
3.在urls.py中写入路由配置
# from django.contrib import admin
from django.urls import path
from web.views import accounturlpatterns = [# path('admin/', admin.site.urls),# 登录界面path('login/', account.login, name="login"), # 短信登录界面path('sms/login/', account.sms_login, name="sms_login"),
]
4.在view文件夹中创建视图函数,account.py,并且在templates文件下创建两个前端界面
# 写入urls所需要的视图函数from django.shortcuts import render, redirectdef login(request):return render(request,'login.html')def sms_login(request):return render(request,'sms_login.html')
5.设计本项目的数据库表(在models.py中)
from django.db import modelsclass ActiveBaseModel(models.Model):active = models.SmallIntegerField(verbose_name="状态", default=1, choices=((1, "激活"), (0, "删除"),))class Meta:abstract = Trueclass Administrator(ActiveBaseModel):""" 管理员表 """username = models.CharField(verbose_name="用户名", max_length=32, db_index=True)password = models.CharField(verbose_name="密码", max_length=64)mobile = models.CharField(verbose_name="手机号", max_length=11, db_index=True)create_date = models.DateTimeField(verbose_name="创建日期", auto_now_add=True)class Level(ActiveBaseModel):""" 级别表 """title = models.CharField(verbose_name="标题", max_length=32)percent = models.IntegerField(verbose_name="折扣")class Customer(ActiveBaseModel):""" 客户表 """username = models.CharField(verbose_name="用户名", max_length=32, db_index=True)password = models.CharField(verbose_name="密码", max_length=64)mobile = models.CharField(verbose_name="手机号", max_length=11, db_index=True)balance = models.DecimalField(verbose_name="账户余额", default=0, max_digits=10, decimal_places=2)level = models.ForeignKey(verbose_name="级别", to="Level", on_delete=models.CASCADE)create_date = models.DateTimeField(verbose_name="创建日期", auto_now_add=True)creator = models.ForeignKey(verbose_name="创建者", to="Administrator", on_delete=models.CASCADE)class PricePolicy(models.Model):""" 价格策略(原价,后续可以根据用级别不同做不同折扣)1 1000 102 2000 18"""count = models.IntegerField(verbose_name="数量")price = models.DecimalField(verbose_name="价格", default=0, max_digits=10, decimal_places=2)class Order(ActiveBaseModel):""" 订单表 """status_choices = ((1, "待执行"),(2, "正在执行"),(3, "已完成"),(4, "失败"),)status = models.SmallIntegerField(verbose_name="状态", choices=status_choices, default=1)# 202211022123123123oid = models.CharField(verbose_name="订单号", max_length=64, unique=True)url = models.URLField(verbose_name="视频地址", db_index=True)count = models.IntegerField(verbose_name="数量")price = models.DecimalField(verbose_name="价格", default=0, max_digits=10, decimal_places=2)real_price = models.DecimalField(verbose_name="实际价格", default=0, max_digits=10, decimal_places=2)old_view_count = models.CharField(verbose_name="原播放量", max_length=32, default="0")create_datetime = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)customer = models.ForeignKey(verbose_name="客户", to="Customer", on_delete=models.CASCADE)memo = models.TextField(verbose_name="备注", null=True, blank=True)class TransactionRecord(ActiveBaseModel):""" 交易记录 """charge_type_class_mapping = {1: "success",2: "danger",3: "default",4: "info",5: "primary",}charge_type_choices = ((1, "充值"), (2, "扣款"), (3, "创建订单"), (4, "删除订单"), (5, "撤单"),)charge_type = models.SmallIntegerField(verbose_name="类型", choices=charge_type_choices)customer = models.ForeignKey(verbose_name="客户", to="Customer", on_delete=models.CASCADE)amount = models.DecimalField(verbose_name="金额", default=0, max_digits=10, decimal_places=2)creator = models.ForeignKey(verbose_name="管理员", to="Administrator", on_delete=models.CASCADE, null=True, blank=True)order_oid = models.CharField(verbose_name="订单号", max_length=64, null=True, blank=True, db_index=True)create_datetime = models.DateTimeField(verbose_name="交易时间", auto_now_add=True)memo = models.TextField(verbose_name="备注", null=True, blank=True)
最好在终端进行表的提交
python manage.py makemigrationspython manage.py migrate