zoukankan      html  css  js  c++  java
  • django xadmin 安装和使用

    版本: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自定义

    官网下载:http://fontawesome.io/

    下载后覆盖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自带的日志记录。用于审计操作者的操作内容。

  • 相关阅读:
    【并发编程】并发编程随笔索引
    主题记录
    Java技术栈思维导图
    【ES 6.5.4 】ElasticSearch知识点
    Logstash Oracle同步设置
    【ES】【Reference 7.8 译】官方文档Mapping说明
    【ES】【Java High Level REST Client】官方索引和文档操作指导
    【ES】JAVA集成ES与基本操作及官方查询指导
    【实践】Effective Java实践总结
    【MyBatis】Spring集成MyBatis示例
  • 原文地址:https://www.cnblogs.com/shhnwangjian/p/6372503.html
Copyright © 2011-2022 走看看