Django+Vue打造購物網站(二)
阿新 • • 發佈:2018-11-19
配置後臺管理
xadmin直接使用之前的線上教育的那個就可以了
users/adminx.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/9/19 下午 01:15 # @Author : gao # @File : adminx.py import xadmin from users.models import VerifyCode from xadmin import views class BaseSetting(object): enable_themes = True use_bootswatch = True class GlobalSettings(object): site_title = "慕學生鮮後臺" site_footer = "mxshop" menu_style = "accordion" class VerifyCodeAdmin(object): list_display = ['code', 'mobile', "add_time"] xadmin.site.register(VerifyCode, VerifyCodeAdmin) xadmin.site.register(views.BaseAdminView, BaseSetting) xadmin.site.register(views.CommAdminView, GlobalSettings)
goods/adminx.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/9/19 下午 01:15 # @Author : gao # @File : adminx.py import xadmin from goods.models import Goods, GoodsCategory, Banner, GoodsCategoryBrand, HotSearchWords, IndexAd, GoodsImage class GoodsAdmin(object): list_display = ["name", "click_num", "sold_num", "fav_num", "goods_num", "market_price", "shop_price", "goods_brief", "goods_desc", "is_new", "is_hot", "add_time"] search_fields = ['name', ] list_editable = ["is_hot", ] list_filter = ["name", "click_num", "sold_num", "fav_num", "goods_num", "market_price", "shop_price", "is_new", "is_hot", "add_time", "category__name"] style_fields = {"goods_desc": "ueditor"} class GoodsImagesInline(object): model = GoodsImage exclude = ["add_time"] extra = 1 style = 'tab' inlines = [GoodsImagesInline] class GoodsCategoryAdmin(object): list_display = ["name", "category_type", "parent_category", "add_time"] list_filter = ["category_type", "parent_category", "name"] search_fields = ['name', ] class GoodsBrandAdmin(object): list_display = ["category", "image", "name", "desc"] def get_context(self): context = super(GoodsBrandAdmin, self).get_context() if 'form' in context: context['form'].fields['category'].queryset = GoodsCategory.objects.filter(category_type=1) return context class BannerGoodsAdmin(object): list_display = ["goods", "image", "index"] class HotSearchAdmin(object): list_display = ["keywords", "index", "add_time"] class IndexAdAdmin(object): list_display = ["category", "goods"] xadmin.site.register(Goods, GoodsAdmin) xadmin.site.register(GoodsCategory, GoodsCategoryAdmin) xadmin.site.register(Banner, BannerGoodsAdmin) xadmin.site.register(GoodsCategoryBrand, GoodsBrandAdmin) xadmin.site.register(HotSearchWords, HotSearchAdmin) xadmin.site.register(IndexAd, IndexAdAdmin)
trade/adminx.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/9/19 下午 01:15 # @Author : gao # @File : adminx.py import xadmin from goods.models import Goods, GoodsCategory, Banner, GoodsCategoryBrand, HotSearchWords, IndexAd, GoodsImage class GoodsAdmin(object): list_display = ["name", "click_num", "sold_num", "fav_num", "goods_num", "market_price", "shop_price", "goods_brief", "goods_desc", "is_new", "is_hot", "add_time"] search_fields = ['name', ] list_editable = ["is_hot", ] list_filter = ["name", "click_num", "sold_num", "fav_num", "goods_num", "market_price", "shop_price", "is_new", "is_hot", "add_time", "category__name"] style_fields = {"goods_desc": "ueditor"} class GoodsImagesInline(object): model = GoodsImage exclude = ["add_time"] extra = 1 style = 'tab' inlines = [GoodsImagesInline] class GoodsCategoryAdmin(object): list_display = ["name", "category_type", "parent_category", "add_time"] list_filter = ["category_type", "parent_category", "name"] search_fields = ['name', ] class GoodsBrandAdmin(object): list_display = ["category", "image", "name", "desc"] def get_context(self): context = super(GoodsBrandAdmin, self).get_context() if 'form' in context: context['form'].fields['category'].queryset = GoodsCategory.objects.filter(category_type=1) return context class BannerGoodsAdmin(object): list_display = ["goods", "image", "index"] class HotSearchAdmin(object): list_display = ["keywords", "index", "add_time"] class IndexAdAdmin(object): list_display = ["category", "goods"] xadmin.site.register(Goods, GoodsAdmin) xadmin.site.register(GoodsCategory, GoodsCategoryAdmin) xadmin.site.register(Banner, BannerGoodsAdmin) xadmin.site.register(GoodsCategoryBrand, GoodsBrandAdmin) xadmin.site.register(HotSearchWords, HotSearchAdmin) xadmin.site.register(IndexAd, IndexAdAdmin)
user_operation/adminx.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/9/19 下午 01:15
# @Author : gao
# @File : adminx.py
import xadmin
from goods.models import Goods, GoodsCategory, Banner, GoodsCategoryBrand, HotSearchWords, IndexAd, GoodsImage
class GoodsAdmin(object):
list_display = ["name", "click_num", "sold_num", "fav_num", "goods_num", "market_price",
"shop_price", "goods_brief", "goods_desc", "is_new", "is_hot", "add_time"]
search_fields = ['name', ]
list_editable = ["is_hot", ]
list_filter = ["name", "click_num", "sold_num", "fav_num", "goods_num", "market_price",
"shop_price", "is_new", "is_hot", "add_time", "category__name"]
style_fields = {"goods_desc": "ueditor"}
class GoodsImagesInline(object):
model = GoodsImage
exclude = ["add_time"]
extra = 1
style = 'tab'
inlines = [GoodsImagesInline]
class GoodsCategoryAdmin(object):
list_display = ["name", "category_type", "parent_category", "add_time"]
list_filter = ["category_type", "parent_category", "name"]
search_fields = ['name', ]
class GoodsBrandAdmin(object):
list_display = ["category", "image", "name", "desc"]
def get_context(self):
context = super(GoodsBrandAdmin, self).get_context()
if 'form' in context:
context['form'].fields['category'].queryset = GoodsCategory.objects.filter(category_type=1)
return context
class BannerGoodsAdmin(object):
list_display = ["goods", "image", "index"]
class HotSearchAdmin(object):
list_display = ["keywords", "index", "add_time"]
class IndexAdAdmin(object):
list_display = ["category", "goods"]
xadmin.site.register(Goods, GoodsAdmin)
xadmin.site.register(GoodsCategory, GoodsCategoryAdmin)
xadmin.site.register(Banner, BannerGoodsAdmin)
xadmin.site.register(GoodsCategoryBrand, GoodsBrandAdmin)
xadmin.site.register(HotSearchWords, HotSearchAdmin)
xadmin.site.register(IndexAd, IndexAdAdmin)
後臺選單變中文
兩種方式,主要根據INSTALLED_APPS
來決定
第一種
使用config來配置
'users.apps.UsersConfig',
users/apps.py
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
verbose_name = "使用者管理"
第二種
'users',
users/apps.py
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
verbose_name = "使用者管理"
users/__ini__.py
default_app_config = 'users.apps.UsersConfig'
具體使用哪種可自行決定,並修改其它三處
user_operation/apps.py
from django.apps import AppConfig
class UserOperationConfig(AppConfig):
name = 'user_operation'
verbose_name = "使用者操作管理"
trade/apps.py
from django.apps import AppConfig
class TradeConfig(AppConfig):
name = 'trade'
verbose_name = "交易管理"
goods/apps.py
from django.apps import AppConfig
class GoodsConfig(AppConfig):
name = 'goods'
verbose_name = "商品"
匯入資料
由於分類和商品很多,就寫個指令碼匯入資料
單獨使用django的model,批量匯入資料
db_tools下新建資料夾data,將category_data.py和product_data.py拷貝到這裡面
把圖片都拷貝到media目錄下
db_tools目錄下新建檔案 import_category_data.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/9/19 下午 04:01
# @Author : gao
# @File : import_category_data.py
# 獨立使用django的model
import sys
import os
# 獲取當前檔案的路徑(執行指令碼)
pwd = os.path.dirname(os.path.realpath(__file__))
# 獲取專案的根目錄
sys.path.append(pwd + "../")
# 要想單獨使用django的model,必須指定一個環境變數,會去settings配置找
# 參照manage.py裡面就知道為什麼這樣設定了
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MxShop.settings")
import django
django.setup()
from goods.models import GoodsCategory
from db_tools.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()
db_tools目錄下新建檔案import_goods_data.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/9/19 下午 04:02
# @Author : gao
# @File : import_goods_data.py
import sys
import os
pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd+"../")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MxShop.settings")
import django
django.setup()
from goods.models import Goods, GoodsCategory, GoodsImage
from db_tools.data.product_data import row_data
for goods_detail in row_data:
goods = Goods()
goods.name = goods_detail["name"]
#前端中是“¥232”,資料庫中是float型別,所以要替換掉
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 ""
#取最後一個
category_name = goods_detail["categorys"][-1]
# 取出當前子類對應的GoodsCategory物件,filter沒有匹配的會返回空陣列,不會拋異常。
category = GoodsCategory.objects.filter(name=category_name)
if category:
goods.category = category[0]
goods.save()
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()
執行這兩個指令碼檔案,將資料新增到資料庫中
配置media路徑
settings.py
# 設定上傳檔案的路徑
MEDIA_URL="/media/"
MEDIA_ROOT=os.path.join(BASE_DIR,"media")
urls.py
from django.views.static import serve
import xadmin
from django.urls import path, include
from MxShop.settings import MEDIA_ROOT
urlpatterns = [
path('admin/', xadmin.site.urls),
path('ueditor/', include('DjangoUeditor.urls')),
path('media/<path:path>', serve, {'document_root': MEDIA_ROOT}),
]