3.1 項(xiàng)目初始化
創(chuàng)建虛擬環(huán)境
pyenv virtualenv 3.6.5 vue_drf
激活虛擬環(huán)境
pyenv activate vue_drf
安裝django和django rest framework
pip install django
pip install djangorestframework
pip install markdown django-filter
rest framework支持的
- Python (3.6, 3.7, 3.8, 3.9, 3.10)
- Django (2.2, 3.0, 3.1, 3.2, 4.0)
修改數(shù)據(jù)庫配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'vue_shop',
'USER': 'root',
'PASSWORD': 'liulunan',
'HOST': 'localhost',
'PORT': '3306'
}
}
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
pip install mysqlclient
報(bào)錯(cuò)
django.db.utils.OperationalError:
(1193, "Unknown system variable 'storage_engine'")
解決
'OPTIONS': {'init_command':
'SET storage_engine=INNODB;'}
修改為
'OPTIONS': {'init_command':
'SET default_storage_engine=INNODB;'}
windows python安裝容易出錯(cuò)的包:www.lfd.uci.edu/~gohlke/pythonlibs/
# 上傳圖片,處理圖片
pip install pillow
整理目錄結(jié)構(gòu)
1.創(chuàng)建apps packages
2.創(chuàng)建extra_apps packages
# 為了直接導(dǎo)入apps目錄下的文件添坊,不用帶apps
import sys
sys.path.insert(0, BASE_DIR)
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))
創(chuàng)建app
python manage.py startapp goods
python manage.py startapp trade # 交易相關(guān)
python manage.py startapp user_operation # 用戶操作
app移入apps文件夾中并加到配置文件中
INSTALLED_APPS = [
...
'users',
'goods',
'trade',
'user_operation',
]
3.2編寫表
3.2.1 用戶表
from django.db import models
from django.contrib.auth.models import AbstractUser
class UserProfile(AbstractUser):
"""
用戶
"""
GENDER_CHOICES = (
('male', u'男'),
('female', '女')
)
username = models.CharField(max_length=20, null=True, blank=True, verbose_name='姓名')
birthday = models.DateField(null=True, blank=True, verbose_name='出生年月')
gender = models.CharField(max_length=6, choices=GENDER_CHOICES, default='male', verbose_name='性別')
mobile = models.CharField(max_length=11, verbose_name='電話')
email = models.EmailField(max_length=100, null=True, blank=True, verbose_name='郵箱')
class Meta:
verbose_name = '用戶'
verbose_name_plural = verbose_name
def __str__(self):
return self.username
替換系統(tǒng)用戶
# settings.py
AUTH_USER_MODEL = 'users.UserProfile'
3.2.2 驗(yàn)證碼表
class VerifyCode(models.Model):
"""
短信驗(yàn)證碼
"""
code = models.CharField(max_length=10, verbose_name='驗(yàn)證碼')
mobile = models.CharField(max_length=11, verbose_name='手機(jī)')
created_time = models.DateTimeField(default=datetime.now, verbose_name='添加時(shí)間')
class Meta:
verbose_name = '短信驗(yàn)證碼'
verbose_name_plural = verbose_name
def __str__(self):
return self.code
3.2.3 商品分類表
class GoodsCategory(models.Model):
"""
商品分類
此項(xiàng)目分為三類
"""
SHOP_CATEGORY_TYPE = (
(1, '一級(jí)類目'),
(2, '二級(jí)類目'),
(3, '三級(jí)類目')
)
name = models.CharField(max_length=30, null=True, blank=True, verbose_name='商品類別名', help_text='商品類別名')
# 用于英文查找
code = models.CharField(max_length=30, null=True, blank=True, verbose_name='商品類別code', help_text='商品類別code')
desc = models.TextField(max_length=300, null=True, blank=True, verbose_name='商品類別描述', help_text='商品類別描述')
category_type = models.IntegerField(choices=SHOP_CATEGORY_TYPE, verbose_name='類目級(jí)別', help_text='類目級(jí)別')
# 多級(jí)關(guān)聯(lián)箫锤,相當(dāng)于樹的上下級(jí)---可以一直關(guān)聯(lián)---self是django中關(guān)聯(lián)自己模型的
# related_name--->查詢用,后期講解且學(xué)習(xí)
parent_category = models.ForeignKey('self', verbose_name='父級(jí)類別', help_text='父級(jí)類別', related_name='sub_cat',
on_delete=models.CASCADE, null=True, blank=True)
# 主頁中導(dǎo)航處顯示的商品分類
is_tab = models.BooleanField(default=False, verbose_name='是否導(dǎo)航', help_text='是否導(dǎo)航')
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加時(shí)間")
class Meta:
verbose_name = '商品分類'
verbose_name_plural = verbose_name
def __str__(self):
return self.name
3.2.4 品牌名
class GoodsCategoryBrand(models.Model):
"""
品牌名
"""
category = models.ForeignKey(GoodsCategory, related_name='brands', null=True, blank=True, verbose_name="商品類目",
on_delete=models.CASCADE)
name = models.CharField(max_length=30, null=True, blank=True, verbose_name='品牌名', help_text='品牌名')
# 上傳地址為media/brand/images/ 數(shù)據(jù)庫中存儲(chǔ)為char類型谚攒,所以要設(shè)置最長值
image = models.ImageField(max_length=200, upload_to='brands/')
created_time = models.DateTimeField(default=datetime.now, verbose_name='添加時(shí)間')
class Meta:
verbose_name = '品牌名'
verbose_name_plural = verbose_name
def __str__(self):
return self.name
3.2.5 商品表
from DjangoUeditor.models import UEditorField
class Goods(models.Model):
"""
商品
"""
# models.CASCADE ---> 級(jí)聯(lián)刪除, Django模擬SQL約束ON DELETE CASCADE的行為,并刪除包含 ForeignKey 的對(duì)象
category = models.ForeignKey(GoodsCategory, verbose_name="商品類目", on_delete=models.CASCADE)
# 售貨員憑借上屏貨號(hào)拿貨的
goods_sn = models.CharField(max_length=50, default="", verbose_name="商品唯一貨號(hào)")
name = models.CharField(max_length=100, verbose_name="商品名", default='')
click_num = models.IntegerField(default=0, verbose_name="點(diǎn)擊數(shù)")
# 賣出的多少件
sold_num = models.IntegerField(default=0, verbose_name="商品銷售量")
fav_num = models.IntegerField(default=0, verbose_name="收藏?cái)?shù)")
goods_num = models.IntegerField(default=0, verbose_name="庫存數(shù)")
market_price = models.FloatField(default=0, verbose_name="市場(chǎng)價(jià)格")
shop_price = models.FloatField(default=0, verbose_name="本店價(jià)格")
goods_brief = models.TextField(max_length=500, verbose_name="商品簡短描述")
# DjangoUeditor的UEditorField使用
goods_desc = UEditorField(verbose_name=u"內(nèi)容", imagePath="goods/images/", width=1000, height=300,
filePath="goods/files/", default='')
# 是否免運(yùn)費(fèi)
ship_free = models.BooleanField(default=True, verbose_name="是否承擔(dān)運(yùn)費(fèi)")
goods_front_image = models.ImageField(upload_to="goods/images/", null=True, blank=True, verbose_name="封面圖")
# 是否為剛出爐的新品
is_new = models.BooleanField(default=False, verbose_name="是否新品")
# 右邊顯示的是否為熱賣商品
is_hot = models.BooleanField(default=False, verbose_name="是否熱銷")
created_time = models.DateTimeField(default=datetime.now, verbose_name="添加時(shí)間")
class Meta:
verbose_name = '商品'
verbose_name_plural = verbose_name
def __str__(self):
return self.name
配置文件中設(shè)置富文本DjangoUeditor
INSTALLED_APPS = [
...
'DjangoUeditor',
]
3.2.6 商品詳情頁輪播圖
class GoodsImage(models.Model, GoodsBase):
"""
商品詳情頁輪播圖
"""
goods = models.ForeignKey(Goods, verbose_name="商品", related_name="images", on_delete=models.CASCADE)
image = models.ImageField(upload_to="", verbose_name="圖片", null=True, blank=True)
class Meta:
verbose_name = '商品圖片'
verbose_name_plural = verbose_name
def __str__(self):
return self.goods.name
3.2.7 主頁面大的輪播的商品
class Banner(models.Model, GoodsBase):
"""
主頁面大的輪播的商品
"""
goods = models.ForeignKey(Goods, verbose_name="商品", on_delete=models.CASCADE)
image = models.ImageField(upload_to='banner/', verbose_name="輪播圖片")
index = models.IntegerField(default=0, verbose_name="輪播順序")
class Meta:
verbose_name = '輪播商品'
verbose_name_plural = verbose_name
def __str__(self):
return self.goods.name
3.2.8 交易相關(guān)表
from datetime import datetime
from django.db import models
from django.contrib.auth import get_user_model
from goods.models import Goods
# 獲取用戶模型
User = get_user_model()
class ShoppingCart(models.Model):
"""
購物車
"""
user = models.ForeignKey(User, verbose_name=u"用戶", on_delete=models.CASCADE)
goods = models.ForeignKey(Goods, verbose_name=u"商品", on_delete=models.CASCADE)
nums = models.IntegerField(default=0, verbose_name="購買數(shù)量")
create_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加時(shí)間")
class Meta:
verbose_name = '購物車'
verbose_name_plural = verbose_name
def __str__(self):
return "%s(%d)".format(self.goods.name, self.nums)
class OrderInfo(models.Model):
"""
訂單
"""
ORDER_STATUS = (
("TRADE_SUCCESS", "成功"),
("TRADE_CLOSED", "超時(shí)關(guān)閉"),
("WAIT_BUYER_PAY", "交易創(chuàng)建"),
("TRADE_FINISHED", "交易結(jié)束"),
("paying", "待支付"),
)
user = models.ForeignKey(User, verbose_name="用戶", on_delete=models.CASCADE)
order_sn = models.CharField(max_length=30, null=True, blank=True, unique=True, verbose_name="訂單號(hào)")
# 支付寶支付時(shí)溺职,支付寶生成訂單號(hào)返回給我們,我們把它與本系統(tǒng)的訂單號(hào)關(guān)聯(lián)
trade_no = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name=u"交易號(hào)")
# 支付狀態(tài)
pay_status = models.CharField(choices=ORDER_STATUS, default="paying", max_length=30, verbose_name="訂單狀態(tài)")
post_script = models.CharField(max_length=200, verbose_name="訂單留言")
order_mount = models.FloatField(default=0.0, verbose_name="訂單金額")
pay_time = models.DateTimeField(null=True, blank=True, verbose_name="支付時(shí)間")
# 用戶信息
address = models.CharField(max_length=100, default="", verbose_name="收貨地址")
signer_name = models.CharField(max_length=20, default="", verbose_name="簽收人")
singer_mobile = models.CharField(max_length=11, verbose_name="聯(lián)系電話")
created_time = models.DateTimeField(default=datetime.now, verbose_name="添加時(shí)間")
class Meta:
verbose_name = "訂單"
verbose_name_plural = verbose_name
def __str__(self):
return str(self.order_sn)
class OrderGoods(models.Model):
"""
訂單的商品詳情
"""
order = models.ForeignKey(OrderInfo, verbose_name="訂單信息", related_name="goods", on_delete=models.CASCADE)
goods = models.ForeignKey(Goods, verbose_name="商品", on_delete=models.CASCADE)
goods_num = models.IntegerField(default=0, verbose_name="商品數(shù)量")
created_time = models.DateTimeField(default=datetime.now, verbose_name="添加時(shí)間")
class Meta:
verbose_name = "訂單商品"
verbose_name_plural = verbose_name
def __str__(self):
return str(self.order.order_sn)
3.2.9 用戶操作相關(guān)的
from datetime import datetime
from django.db import models
from django.contrib.auth import get_user_model
from goods.models import Goods
User = get_user_model()
class UserFav(models.Model):
"""
用戶收藏
"""
user = models.ForeignKey(User, verbose_name="用戶", on_delete=models.CASCADE)
goods = models.ForeignKey(Goods, verbose_name="商品", help_text="商品id", on_delete=models.CASCADE)
add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加時(shí)間")
class Meta:
verbose_name = '用戶收藏'
verbose_name_plural = verbose_name
# 聯(lián)合唯一索引
unique_together = ("user", "goods")
def __str__(self):
return self.user.username
class UserLeavingMessage(models.Model):
"""
用戶留言
"""
MESSAGE_CHOICES = (
(1, "留言"),
(2, "投訴"),
(3, "詢問"),
(4, "售后"),
(5, "求購")
)
user = models.ForeignKey(User, verbose_name="用戶", on_delete=models.CASCADE)
message_type = models.IntegerField(default=1, choices=MESSAGE_CHOICES, verbose_name="留言類型",
help_text=u"留言類型: 1(留言),2(投訴),3(詢問),4(售后),5(求購)")
subject = models.CharField(max_length=100, default="", verbose_name="主題")
message = models.TextField(default="", verbose_name="留言內(nèi)容", help_text="留言內(nèi)容")
file = models.FileField(upload_to="message/images/", verbose_name="上傳的文件", help_text="上傳的文件")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加時(shí)間")
class Meta:
verbose_name = "用戶留言"
verbose_name_plural = verbose_name
def __str__(self):
return self.subject
class UserAddress(models.Model):
"""
用戶收貨地址
"""
user = models.ForeignKey(User, verbose_name="用戶", on_delete=models.CASCADE)
province = models.CharField(max_length=100, default="", verbose_name="省份")
city = models.CharField(max_length=100, default="", verbose_name="城市")
district = models.CharField(max_length=100, default="", verbose_name="區(qū)域")
address = models.CharField(max_length=100, default="", verbose_name="詳細(xì)地址")
signer_name = models.CharField(max_length=100, default="", verbose_name="簽收人")
signer_mobile = models.CharField(max_length=11, default="", verbose_name="電話")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加時(shí)間")
class Meta:
verbose_name = "收貨地址"
verbose_name_plural = verbose_name
def __str__(self):
return self.address
3.3 migrate遷移表
報(bào)錯(cuò)
users.UserProfile: (auth.E003) 'UserProfile.username'
must be unique because it is named as the 'USERNAME_FIELD'
解決方案
在重寫django的User model時(shí)浪耘,需要將username設(shè)置為unique=True塑崖,
否則會(huì)報(bào)這個(gè)錯(cuò)誤。
makemigrations
migrate
django migrations表記錄了執(zhí)行過哪些py文件规婆,如果表中有了就會(huì)跳過澜躺。如果想重新遷移兩步操作:
- 刪除表
- 刪除django migrations表中的記錄
3.4 xadmin后臺(tái)管理系統(tǒng)的配置
拷貝xadmin.py文件
在settings.py 加上xadmin
INSTALLED_APPS = [
...
'crispy_forms',
'xadmin',
]
安裝xadmin依賴包抒蚜,在github上搜索xadmin,找到requirements.txt中沒有安裝的包安裝
pip install django-crispy-forms django-import-export django-reversion django-formtools future httplib2 six
還有兩個(gè)包沒裝,excel文件導(dǎo)出用的,xlwt,xlsxwriter
pip install xlwt xlsxwriter
xadmin配置一個(gè)訪問路徑
import xadmin
urlpatterns = [
path('admin/', xadmin.site.urls),
]
創(chuàng)建超級(jí)用戶
python manage.py createsuperuser
報(bào)錯(cuò)
ImportError: cannot import name 'six
修改:extra_apps/xadmin/sites.py文件from django.utils import six修改改為import six
import six
然后又報(bào)錯(cuò)
ImportError: cannot import name 'python_2_unicode_compatible'
修改為
from six import python_2_unicode_compatible
ImportError: cannot import name 'FieldDoesNotExist'
把from django.db.models.fields import FieldDoesNotExist
改為from django.core.exceptions import FieldDoesNotExist
https://blog.csdn.net/qq_38315711/article/details/110650279
https://blog.51cto.com/u_14234228/2604924
https://www.cnblogs.com/hehecat/p/9358033.html
https://blog.csdn.net/huanhuanq1209/article/details/77884014
后臺(tái)app名稱英文改為中文
# apps/goods/apps.py
from django.apps import AppConfig
class GoodsConfig(AppConfig):
name = 'goods'
# 后臺(tái)管理應(yīng)用顯示
verbose_name = '商品'
xadmin配置和ueditor富文本顯示
class GoodsAdmin(object):
style_fields = {"goods_desc": "ueditor"}
3.5 導(dǎo)入商品數(shù)據(jù)
3.5.1 導(dǎo)入商品類別數(shù)據(jù)
# 獨(dú)立使用django的model
import os
import sys
# 獲取當(dāng)前文件路徑
pwd = os.path.dirname(os.path.realpath(__file__))
# 將項(xiàng)目根目錄加入到python根搜索目錄下嗡髓,和setting一樣
# 相對(duì)路徑:pwd+'../'
sys.path.append(pwd + '../')
# 根據(jù)項(xiàng)目目錄查找setting中的配置
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "VueShop.settings")
import django
django.setup()
from goods.models import GoodsCategory
from common_tools.data_import.data.category_data import row_data
for lev1_cat in row_data:
lev1_intance = GoodsCategory()
lev1_intance.code = lev1_cat["code"]
lev1_intance.name = lev1_cat["name"]
lev1_intance.category_type = 1
lev1_intance.save()
for lev2_cat in lev1_cat["sub_categorys"]:
lev2_intance = GoodsCategory()
lev2_intance.code = lev2_cat["code"]
lev2_intance.name = lev2_cat["name"]
lev2_intance.category_type = 2
lev2_intance.parent_category = lev1_intance
lev2_intance.save()
for lev3_cat in lev2_cat["sub_categorys"]:
lev3_intance = GoodsCategory()
lev3_intance.code = lev3_cat["code"]
lev3_intance.name = lev3_cat["name"]
lev3_intance.category_type = 3
lev3_intance.parent_category = lev2_intance
lev3_intance.save()
3.5.2 導(dǎo)入商品數(shù)據(jù)和商品詳情頁輪播圖
# -*- coding: utf-8 -*-
import sys
import os
pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + "../")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "VueShop.settings")
import django
django.setup()
from goods.models import Goods, GoodsCategory, GoodsImage
from common_tools.data_import.data.product_data import row_data
goods_sn = 1
for goods_detail in row_data:
goods = Goods()
goods.name = goods_detail["name"]
goods.market_price = float(int(goods_detail["market_price"].replace("¥", "").replace("元", "")))
goods.shop_price = float(int(goods_detail["sale_price"].replace("¥", "").replace("元", "")))
goods.goods_brief = goods_detail["desc"] if goods_detail["desc"] is not None else ""
goods.goods_desc = goods_detail["goods_desc"] if goods_detail["goods_desc"] is not None else ""
goods.goods_front_image = goods_detail["images"][0] if goods_detail["images"] else ""
goods.goods_sn = goods_sn
category_name = goods_detail["categorys"][-1]
category = GoodsCategory.objects.filter(name=category_name)
if category:
goods.category = category[0]
goods.save()
goods_sn += 1
for goods_image in goods_detail["images"]:
goods_image_instance = GoodsImage()
goods_image_instance.image = goods_image
goods_image_instance.goods = goods
goods_image_instance.save()