Django自带有admin后台,但是其风格并不漂亮、功能也不是让人很满意。因此一些大牛就重写了admin后台叫做xadmin,进来的各位应该是对django自带的admin不是很满意,可以参照我的教程来一次替换。
项目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
在每个APP目录下新建一个adminx.py文件,这里以订单order为例。
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
必须叫adminx.py
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