Django自带有admin后台,但是其风格并不漂亮、功能也不是让人很满意。因此一些大牛就重写了admin后台叫做xadmin,进来的各位应该是对django自带的admin不是很满意,可以参照我的教程来一次替换。
注意django版本不可使用django 2.1及以上
项目github地址为https://github.com/sshwsfc/xadmin,其中的xadmin文件夹是源码文件夹。
如果网速不快,可以访问我提供的链接,直接下载源码和安装依赖环境requirements.txt。https://pan.baidu.com/s/11ks1PsEl13V1H4W2vBd-Bw
应将xadmin放在项目中与static目录同,放置位置如图所示。
NSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'xadmin',#添加
'crispy_forms',#添加
'reversion',#添加(可选)
'df_user',
'df_goods',
'tinymce',
'df_cart',
'df_order',
'haystack',
]
LANGUAGE_CODE = 'zh-hans'#更改(xadmin中文)
TIME_ZONE = 'Asia/Shanghai'#更改
from django.conf.urls import url,include
# from django.contrib import admin 注释admin
#以下四句为新添加内容
import xadmin
xadmin.autodiscover()
from xadmin.plugins import xversion
xversion.register_models()
urlpatterns = [
# url(r'^admin/', admin.site.urls),注释原路由
url(r'^xadmin/',include(xadmin.site.urls)),#添加新路由
url(r'^user/', include('df_user.urls',namespace='user')),
url(r'^', include('df_goods.urls',namespace='goods')),
url(r'^cart/',include('df_cart.urls')),
url(r'^order/',include('df_order.urls')),
]
确保model.py中已经对各个参数设置了别名,这样才能在后台正常显示你所想看到的内容。对属性名进行别名设置只需要在参数位置加上verbose_name=”想要的名字”即可,对类设置别名则需要定义一个子类。
class TypeInfo(models.Model):
ttitle = models.CharField(max_length=20,verbose_name="类别")
isDelete = models.BooleanField(default=False)
def __str__(self):
return self.ttitle
class Meta:
verbose_name = '商品类别'
verbose_name_plural = verbose_name
model内容如下:
from django.db import models
# Create your models here.
class Order(models.Model):
oid = models.CharField(max_length=20,primary_key=True,verbose_name="订单号")#订单号
user = models.ForeignKey('df_user.User')
odate = models.DateTimeField(auto_now=True,verbose_name="提交时间")#订单提交时间
oisPay = models.BooleanField(default=False,verbose_name="是否支付")
ototal = models.DecimalField(max_digits=6,decimal_places=2,verbose_name="总金额")#小数为2位,一共6位
oadress = models.CharField(max_length=150,verbose_name="地址")
class Meta:
verbose_name = '订单'
verbose_name_plural = verbose_name
class OrderDetail(models.Model):
goods = models.ForeignKey('df_goods.GoodInfo')
order = models.ForeignKey(Order)
price = models.DecimalField(max_digits=5,decimal_places=2,verbose_name="价格")
count = models.IntegerField(verbose_name="数量")
class Meta:
verbose_name = '订单详情'
verbose_name_plural = verbose_name
import xadmin
from .models import Order,OrderDetail
class OrderAdmin(object):
list_display = ['oid','user' ,'odate','oisPay','ototal',
'oadress']
search_fields = ['oid', ]
list_editable = ['oid','user' ,'oisPay','ototal','oadress' ]
list_filter = ['oid','user' ,'odate','oisPay','ototal','oadress']
class OrderDetailAdmin(object):
list_display = ['goods', 'price', 'count', ]
list_editable = ['goods', 'price', 'count',]
list_filter = ['goods']
xadmin.site.register(Order,OrderAdmin)
xadmin.site.register(OrderDetail,OrderDetailAdmin)
from django.apps import AppConfig
class DfOrderConfig(AppConfig):
name = 'df_order'
verbose_name = "订单"
default_app_config='df_order.apps.DfOrderConfig'
如果前面对model进行了修改,应该将数据库移除,从新建表。
(venv) root@coding:/opt/duke/b2c-shop# python manage.py makemigrations
(venv) root@coding:/opt/duke/b2c-shop# python manage.py migrate
xadmin可以使用的页面样式控制基本与Django原生的admin一直。
model_icon
class SKUAdmin(object):
model_icon = 'fa fa-gift'
xadmin.site.register(models.SKU, SKUAdmin)
可选的图标样式参考http://fontawesome.dashgame.com/
list_display
list_display = ['id', 'name', 'price', 'stock', 'sales', 'comments']
search_fields
search_fields = ['id','name']
list_filter
list_filter = ['category']
list_editable
list_editable = ['price', 'stock']
show_detail_fields
show_detail_fields = ['name']
list_export
list_export = ['xls', 'csv', 'xml']
refresh_times
lass OrderAdmin(object):
list_display = ['order_id', 'create_time', 'total_amount', 'pay_method', 'status']
refresh_times = [3, 5] # 可选以支持按多长时间(秒)刷新页面