「Django」Xadmin應用


第一:命令安裝xadmin2css

pip install xadmin2

第二:setting.py中設置 INSTALLED_APPSpython

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),
]

 第四:數據庫同步,命令行下輸入
#數據庫同步命令django

python manage.py makemigrations
python manage.py migrate

第五:將django模塊註冊到xadmin後臺 app

 1.將app註冊到xadmin 
app中新建adminx.py文件url

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)

注:spa

引入自定義的ico 案例
官網下載:http://www.fontawesome.com.cn/ 下載後覆蓋xadmin\static\xadmin下的css、fonts目錄.net

對應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

二、設置admix主題和定製頭尾部並設置左側列表收起code

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後臺顯示中文名

一、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 = '操做模塊'
相關文章
相關標籤/搜索