訂單交易平臺準備工作
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 account
urlpatterns = [
# 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, redirect
def login(request):
return render(request,'login.html')
def sms_login(request):
return render(request,'sms_login.html')
5.設計本專案的資料庫表(在models.py中)
from django.db import models
class ActiveBaseModel(models.Model):
active = models.SmallIntegerField(verbose_name="狀態", default=1, choices=((1, "啟用"), (0, "刪除"),))
class Meta:
abstract = True
class 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 10
2 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)
# 202211022123123123
oid = 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 makemigrations
python manage.py migrate