migrations原理及表生成
前提: 将我们的 app 都放入了 settings 列表中
此时运行 migrations ,我们会报错。future 模块找不到之类。
因为我们拷贝的源码中没有安装依赖包
pip install git+git://github.com/sshwsfc/xadmin.git@django2
因为使用的django2.0.1版本
所以需要对所有的外键关系加上删除时的操作,这里为了方便统一
将删除时操作,设置为级联删除。
在setting中 goods 与 goods.apps.GoodsConfig是一样的。
python manage.py makemigrations #生成每次数据库变动的脚本
python manage.py migrate #生成数据表
注意:不再生成auth user表,而是生成userProfile表。
而admin只会去找auth user就会报错,此时可以先注释掉 admin 这个app和相应 url
xadmin后台管理配置
xadmin 文件配置显示字段
每个 app 配置一个 adminx.py 文件
goods adminx 文件
# encoding: utf-8
__author__ = 'Evan'
import xadmin
from .models import Goods, GoodsCategory, GoodsImage, GoodsCategoryBrand, Banner, HotSearchWords
from .models import IndexAd
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 文件
# -*- coding: utf-8 -*-
__author__ = 'Evan'
import xadmin
from .models import ShoppingCart, OrderInfo, OrderGoods
class ShoppingCartAdmin(object):
list_display = ["user", "goods", "nums", ]
class OrderInfoAdmin(object):
list_display = ["user", "order_sn", "trade_no", "pay_status", "post_script",
"order_mount", "pay_time", "add_time"]
class OrderGoodsInline(object):
model = OrderGoods
exclude = ['add_time', ]
extra = 1
style = 'tab'
inlines = [OrderGoodsInline, ]
xadmin.site.register(ShoppingCart, ShoppingCartAdmin)
xadmin.site.register(OrderInfo, OrderInfoAdmin)
user_operation adminx 文件
# coding: utf-8
__author__ = 'Evan'
import xadmin
from .models import UserAddress, UserFav, UserLeavingMessage
class UserFavAdmin(object):
list_display = ['user', 'goods', 'add_time']
class UserLeavingMessageAdmin(object):
list_display = ['user', 'message_type', 'message', 'add_time']
class UserAddressAdmin(object):
list_display = ['signer_name', 'signer_mobile', 'district', 'address']
xadmin.site.register(UserFav, UserFavAdmin)
xadmin.site.register(UserAddress, UserAddressAdmin)
xadmin.site.register(UserLeavingMessage, UserLeavingMessageAdmin)
user adminx 文件
# coding: utf-8
__author__ = 'Evan'
import xadmin
from xadmin import views
from .models import VerifyCode
class BaseSetting(object):
enable_themes = True
use_bootswatch = True
class GlobalSettings(object):
list_title = "vue_shop"
site_footer = "shop@vue.cn"
# 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)
xadmin配置url
path('xadmin/', xadmin.site.urls),
createsuperuser
pip install xlwt
修改setting中app名称为补全名称
'users.apps.UsersConfig',
'goods.apps.GoodsConfig',
'trade.apps.TradeConfig',
'user_operation.apps.UserOperationConfig',
然后在apps中添加
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
verbose_name = "用户管理"
配置富文本:
# 富文本相关url
path('ueditor/', include('DjangoUeditor.urls')),
导入商品类别数据
db_tools
目录中存放有data和导入的脚本,
图片复制进media
运行两个脚本
为了让大家进行更方便的修改保持字段一致
知识点:单独使用django的model
访问图片settings设置
#设置js/css/image等静态文件目录
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
#设置上传文件,图片访问路径
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
处理图片显示的url
# 处理图片显示的url,使用Django自带serve,
# 传入参数告诉它去哪个路径找,我们有配置好的路径MEDIAROOT
url(r'media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT}),