第一:命令安装xadmin2
pip install xadmin2
第二:setting.py中设置 INSTALLED_APPS
INSTALLED_APPS = ( ... 'xadmin', 'crispy_forms', 'reversion', ... )
第三:urls.py中设置
import xadmin xadmin.autodiscover() from xadmin.plugins import xversion xversion.register_models() urlpatterns = [ path('xadmin/',xadmin.site.urls), ]
第四:数据库同步,命令行下输入
#数据库同步命令
python manage.py makemigrations python manage.py migrate
第五:将django模块注册到xadmin后台
1.将app注册到xadmin
app中新建adminx.py文件
import xadmin from .models import * class relayControlXadmin(object): #后台显示选择 list_display = ['time', 'oneCH', 'twoCH'] #搜索选择 search_fields = ['time', 'oneCH', 'twoCH'] #过滤器选择 list_filter = ['time', 'oneCH', 'twoCH'] #设置后台图标 model_icon = 'fa fa-user' xadmin.site.register(relayControl,relayControlXadmin)
注:
引入自定义的ico 案例
官网下载:http://www.fontawesome.com.cn/ 下载后覆盖xadmin\static\xadmin下的css、fonts目录
对应model
class relayControl(models.Model): time = models.DateTimeField(auto_now=True,verbose_name='操作时间') oneCH = models.BooleanField(verbose_name='一号继电器',null=True,blank=True) twoCH = models.BooleanField(verbose_name='二号继电器',null=True,blank=True) class Meta: verbose_name = '继电器控制' verbose_name_plural = verbose_name def __str__(self): return self.time
2、设置admix主题和定制头尾部并设置左侧列表收起
import xadmin from .models import * from xadmin import views #xadmin后台头尾定制 class GlobalSetting(object): site_title = "****项目后台管理系统" site_footer = "http://www.runge.net/" #左侧的列表紧缩间隔设置 menu_style = "accordion" xadmin.site.register(views.CommAdminView, GlobalSetting) #xadmin后台主题设置 class BaseSetting(object): enable_themes = True use_bootswatch = True xadmin.site.register(views.BaseAdminView, BaseSetting)
第六:App后台显示中文名
1、settings设置
INSTALLED_APPS = [ ... 'users.apps.AppConfig', 'workers.apps.WorkersConfig', 'item.apps.ItemConfig', 'www.apps.WwwConfig', 'control.apps.ControlConfig', 'xadmin', 'crispy_forms', 'reversion' ]
2.各App中apps.py设置
from django.apps import AppConfig class ControlConfig(AppConfig): name = 'control' verbose_name = '操作模块'