第一步安装xadmin-django2
python setup.py install
安装依赖pip install -r requirements.txt
第二步setting配置xadmin
INSTALLED_APPS = [
'xadmin', # 添加
'crispy_forms', # 添加
'reversion', # 添加(可选)
...
]
第三步urls.py配置,xadmin替换admin
1 2 3 4 5 6 7 8 | 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), ] |
---|
数据库同步
1 2 | makemigrations migrate |
---|
下载:https://github.com/sshwsfc/xadmin
解压xadmin-master.zip文件
将xadmin-master文件中xadmin目录复制到自己的django项目下
已在models创建表EmailVerifyRecord
1 2 3 4 5 6 7 8 9 | 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文件
1 2 3 4 5 6 7 8 9 | 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文件增加
1 2 3 4 5 6 7 | from xadmin import views class BaseSetting(object): enable_themes = True use_bootswatch = True xadmin.site.register(views.BaseAdminView, BaseSetting) |
---|
adminx.py文件增加
1 2 3 4 5 6 7 | from xadmin import views class GlobalSetting(object): site_title = "shhnwangjian后台管理系统" site_footer = "http://www.cnblogs.com/shhnwangjian/" xadmin.site.register(views.CommAdminView, GlobalSetting) |
---|
修改前
修改后
adminx.py文件增加
1 2 3 4 5 6 7 8 | 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文件
1 2 3 4 5 6 7 | # -*- coding: utf-8 -*- from django.apps import AppConfig class UsersConfig(AppConfig): name = 'users' verbose_name = u'用户信息' |
---|
__init__.py文件
1 | default_app_config = "users.apps.UsersConfig" |
---|
下载后覆盖css、fonts目录
在adminx.py文件中引入model_icon
1 2 3 4 5 6 7 | 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源码中加入下面内容
1 2 | from django.contrib.auth import get_user_model User = get_user_model() |
---|
修改密码,需要更改源码
adminx.py文件内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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自带的日志记录。用于审计操作者的操作内容。