1,xadmin的主题等一些全局的功能设置,可以设计在一个app中即可,全局变量。
2、创建user 的app 定义user的models
from django.db import models
from datetime import datetime
from goods.models import GoodsInfo,TypeInfo
from stdimage.models import StdImageField
class UserInfo(models.Model):
uname = models.CharField(max_length=20, verbose_name="用户名", unique=True)
upwd = models.CharField(max_length=40, verbose_name="用户密码", blank=False)
uemail = models.EmailField(verbose_name="邮箱", unique=True)
avatar = StdImageField(
default='image/default.jpg',
upload_to="image/%Y/%m",
verbose_name=u"我的头像",
variations={'thumbnail': {'width': 100, 'height': 75}},
max_length=100)
ushou = models.CharField(max_length=20, default="", verbose_name="收货地址")
uaddress = models.CharField(max_length=100, default="", verbose_name="地址")
uyoubian = models.CharField(max_length=6, default="", verbose_name="邮编")
uphone = models.CharField(max_length=11, default="", verbose_name="手机号")
# default,blank是python层面的约束,不影响数据库表结构,修改时不需要迁移 python manage.py makemigrations
class Meta:
verbose_name = "用户信息"
verbose_name_plural = verbose_name
def url(self):
if self.avatar:
return self.avatar.url
else:
return "url为空"
def image_img(self):
if self.avatar:
href = self.avatar.url # 点击后显示的放大图片
src = self.avatar.thumbnail.url # 页面显示的缩略图
# 插入html代码
image_html = '' % (href, src)
return image_html
else:
return '上传图片'
image_img.short_description = '头像图片'
image_img.allow_tags = True # 列表页显示图片
def __str__(self):
return self.uname
class GoodsBrowser(models.Model):
user = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="用户ID")
good = models.ForeignKey(GoodsInfo, on_delete=models.CASCADE, verbose_name="商品ID")
browser_time = models.DateTimeField(default=datetime.now, verbose_name="浏览时间")
class Meta:
verbose_name = "用户浏览记录"
verbose_name_plural = verbose_name
def __str__(self):
return "{0}浏览记录{1}".format(self.user.uname, self.good.gtitle)
# 轮播图
class Banner(models.Model):
title = models.CharField(max_length=100, verbose_name=u"标题")
image = models.ImageField(
upload_to="banner/%Y/%m",
verbose_name=u"轮播图",
max_length=100)
url = models.URLField(max_length=200, verbose_name=u"访问地址")
# 默认index很大靠后。想要靠前修改index值。
index = models.IntegerField(default=100, verbose_name=u"顺序")
add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")
class Meta:
verbose_name = u"轮播图"
verbose_name_plural = verbose_name
# 重载__str__方法使后台不再直接显示object
def __str__(self):
return '{0}(位于第{1}位)'.format(self.title, self.index)
class Advert(models.Model):
adname = models.CharField(max_length=50,verbose_name='广告名称')
adtype = models.ForeignKey(TypeInfo, on_delete=models.CASCADE, verbose_name='广告类别')
adimage = models.ImageField(
upload_to="advert/%Y/%m",
verbose_name=u"侧边广告图",
max_length=100)
adurl = models.URLField(max_length=200, verbose_name=u"访问地址")
# 默认index很大靠后。想要靠前修改index值。
adindex = models.IntegerField(default=100, verbose_name=u"顺序")
ad_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")
class Meta:
verbose_name = u"侧边广告"
verbose_name_plural = verbose_name
# 重载__str__方法使后台不再直接显示object
def __str__(self):
return '{0}(位于第{1}位)'.format(self.adname, self.adindex)
3、user的adminx.py
from django.contrib import admin
# Register your models here.
import xadmin
from order.models import OrderInfo, OrderDetailInfo
from cart.models import CartInfo
from .models import UserInfo, GoodsBrowser, Banner, Advert
from xadmin import views
from goods.models import GoodsInfo, TypeInfo
# X admin的全局配置信息设置
class BaseSetting(object):
# 主题功能开启
enable_themes = True
use_bootswatch = True
# x admin 全局配置参数信息设置
class GlobalSettings(object):
site_title = " 天天生鲜后台管理站"
site_footer = "天天生鲜后台管理站"
# 收起菜单
menu_style = "accordion"
def get_site_menu(self):
return (
{'title': '用户管理', 'menus': (
{'title': '用户信息', 'url': self.get_model_url(UserInfo, 'changelist')},
{'title': '用户浏览记录', 'url': self.get_model_url(GoodsBrowser, 'changelist')},
{'title': '轮播图管理', 'url': self.get_model_url(Banner, 'changelist')},
{'title': '侧边广告管理', 'url': self.get_model_url(Advert, 'changelist')},
)},
{'title': '商品管理', 'menus': (
{'title': '商品信息', 'url': self.get_model_url(GoodsInfo, 'changelist')},
{'title': '商品类别', 'url': self.get_model_url(TypeInfo, 'changelist')},
)},
{'title': '订单管理', 'menus': (
{'title': '订单列表', 'url': self.get_model_url(OrderInfo, 'changelist')},
{'title': '订单明细', 'url': self.get_model_url(OrderDetailInfo, 'changelist')},
)},
{'title': '购物车', 'menus': (
{'title': '购物车', 'url': self.get_model_url(CartInfo, 'changelist')},
)},
)
class UserInfoAdmin(object):
list_display = ["uname", "uemail", "ushou", "uaddress", "uyoubian", "uphone", 'image_img']
list_per_page = 5
list_filter = ["uname", "uyoubian"]
search_fields = ["uname", "uyoubian"]
readonly_fields = ["uname"]
class GoodsBrowserAdmin(object):
list_display = ["user", "good"]
list_per_page = 50
list_filter = ["user__uname", "good__gtitle"]
search_fields = ["user__uname", "good__gtitle"]
readonly_fields = ["user", "good"]
refresh_times = [3, 5]
class BannerAdmin(object):
list_display = ['title', 'image', 'url', 'index', 'add_time']
search_fields = ['title', 'image', 'url', 'index']
list_filter = ['title', 'image', 'url', 'index', 'add_time']
list_display_links = ['title']
class AdvertAdmin(object):
list_display = ['id','adname','adtype', 'adimage', 'adurl', 'adindex', 'ad_time']
search_fields = ['adname', 'url', 'index']
list_filter = ['adname', 'ad_time']
list_display_links = ['adname']
xadmin.site.register(UserInfo, UserInfoAdmin)
xadmin.site.register(GoodsBrowser, GoodsBrowserAdmin)
xadmin.site.register(Banner, BannerAdmin)
xadmin.site.register(Advert, AdvertAdmin)
# 将Xadmin全局管理器与我们的view绑定注册。
xadmin.site.register(views.BaseAdminView, BaseSetting)
# 将头部与脚部信息进行注册:
xadmin.site.register(views.CommAdminView, GlobalSettings)
# X admin的全局配置信息设置 class BaseSetting(object): # 主题功能开启 enable_themes = True use_bootswatch = True # x admin 全局配置参数信息设置 class GlobalSettings(object): site_title = " 天天生鲜后台管理站" site_footer = "天天生鲜后台管理站" # 收起菜单 menu_style = "accordion" def get_site_menu(self):
以及最新要注册xadmin的
# 将Xadmin全局管理器与我们的view绑定注册。 xadmin.site.register(views.BaseAdminView, BaseSetting) # 将头部与脚部信息进行注册: xadmin.site.register(views.CommAdminView, GlobalSettings)
切换主题