全部博文(61)
分类: Python/Ruby
2017-05-18 16:04:09
版本:django1.9
1
|
源码下载,,在django中新建python package名字叫extra_apps,将xadmin文件夹拷贝进去,然后设置为source root(前提是你用pycharm开发)
|
点击(此处)折叠或打开
AUTH_USER_MODEL = 'users.UserProfile'
INSTALLED_APPS中增加2个app:xadmin、crispy_forms、users
1
2
3
4
5
6
7
8
9
10
|
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'xadmin',
'crispy_forms',
'users',
]
|
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
|
已在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这个值,在 中选中的图标或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自带的日志记录。用于审计操作者的操作内容。