版本:django1.9
pip安裝部署
pip install xadmin
- settings.py配置
INSTALLED_APPS中增加2個app:xadmin、crispy_forms
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'xadmin',
'crispy_forms',
]
- urls.py配置,xadmin替換admin
from django.conf.urls import url
from django.contrib import admin
import xadmin
urlpatterns = [
# url(r'^admin/', admin.site.urls),
url(r'^xadmin/', xadmin.site.urls),
]
- 數據庫同步
makemigrations migrate
- 訪問首頁

源碼安裝部署
下載:https://github.com/sshwsfc/xadmin
解壓xadmin-master.zip文件
將xadmin-master文件中xadmin目錄復制到自己的django項目下
xadmin注冊
已在models創建表EmailVerifyRecord
class EmailVerifyRecord(models.Model):
email_choices = (
('register', u'注冊'),
('forget', u'找回密碼'),
)
code = models.CharField(max_length=20, verbose_name=u'驗證碼')
email = models.EmailField(max_length=50, verbose_name=u'郵箱')
send_type = models.CharField(choices=email_choices, max_length=10, verbose_name=u'驗證碼類型')
send_time = models.DateTimeField(default=datetime.now, verbose_name=u'發送時間')
在創建的app下創建adminx.py文件
import xadmin
from .models import EmailVerifyRecord
class EmailVerifyRecordAdmin(object):
list_display = ['code', 'email', 'send_type', 'send_time']
search_fields = ['code', 'email', 'send_type']
list_filter = ['code', 'email', 'send_type', 'send_time']
xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin)
主題應用

adminx.py文件增加
from xadmin import views
class BaseSetting(object):
enable_themes = True
use_bootswatch = True
xadmin.site.register(views.BaseAdminView, BaseSetting)
全局應用
- 頁頭和頁尾自定義

adminx.py文件增加
from xadmin import views
class GlobalSetting(object):
site_title = "shhnwangjian后台管理系統"
site_footer = "http://www.cnblogs.com/shhnwangjian/"
xadmin.site.register(views.CommAdminView, GlobalSetting)
- 左側app中文名稱定義和多表緊縮
修改前

修改后

adminx.py文件增加
from xadmin import views
class GlobalSetting(object):
site_title = "shhnwangjian后台管理系統"
site_footer = "http://www.cnblogs.com/shhnwangjian/"
menu_style = "accordion"
xadmin.site.register(views.CommAdminView, GlobalSetting)
apps.py文件
# -*- coding: utf-8 -*-
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
verbose_name = u'用戶信息'
__init__.py文件
default_app_config = "users.apps.UsersConfig"
font awesome自定義
下載后覆蓋css、fonts目錄

在adminx.py文件中引入model_icon
class EmailVerifyRecordAdmin(object):
list_display = ['code', 'email', 'send_type', 'send_time']
search_fields = ['code', 'email', 'send_type']
list_filter = ['code', 'email', 'send_type', 'send_time']
model_icon = 'fa fa-address-book-o'
xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin)
model_icon這個值,在http://fontawesome.io/icons/ 中選中的圖標或class內容,如下圖

引用自定義的用戶信息

如上圖目錄中xadmin源碼中加入下面內容
from django.contrib.auth import get_user_model User = get_user_model()
修改密碼,需要更改源碼

actions制作案例
adminx.py文件內容
from django.core.exceptions import PermissionDenied
from xadmin.views.base import filter_hook
from xadmin.util import model_format_dict, model_ngettext
class zabbixitmes_display_off_action(BaseActionView):
action_name = "zabbixitmes_display_off_action"
description = u'%(verbose_name_plural)s 展示和采集關閉'
model_perm = 'change'
@filter_hook
def change_models(self, queryset):
n = queryset.count()
if n:
self.log('change', (u' %(count)d %(items)s. 展示和采集關閉')
% {"count": n, "items": model_ngettext(self.opts, n)})
for obj in queryset:
obj.display_insert = 0
obj.save()
def do_action(self, queryset):
if not self.has_change_permission():
raise PermissionDenied
if self.request.POST:
self.change_models(queryset)
return None
提示:可以參考BaseActionView中的刪除DeleteSelectedAction,可以實現xadmin自帶的日志記錄。用於審計操作者的操作內容。


