Django3.0.3使用Xadmin

Django3.0.3使用Xadmin

下载地址

pip3 install https://codeload.github.com/sshwsfc/xadmin/zip/django2 -i https://pypi.douban.com/simple/

注册应用:

INSTALLED_APPS = [
    ...
    'xadmin',
    'crispy_forms',
    'reversion',
    ...
]

# 修改使用中文界面
LANGUAGE_CODE = 'zh-Hans'

# 修改时区
TIME_ZONE = 'Asia/Shanghai'

xadmin有建立自己的数据库模型类,需要进行数据库迁移

python3 manage.py makemigrations
python3 manage.py migrate

如果之前没有创建超级用户,需要创建,如果有了,则可以直接使用之前的。

python manage.py createsuperuser

在总路由中添加xadmin的路由信息

import xadmin
xadmin.autodiscover()

# version模块自动注册需要版本控制的 Model
from xadmin.plugins import xversion
xversion.register_models()

urlpatterns = [
    path(r'xadmin/', xadmin.site.urls),
]

给xadmin设置基本站点配置信息

import xadmin
from xadmin import views

class BaseSetting(object):
    """xadmin的基本配置"""
    enable_themes = True  # 开启主题切换功能
    use_bootswatch = True

xadmin.site.register(views.BaseAdminView, BaseSetting)

class GlobalSettings(object):
    """xadmin的全局配置"""
    site_title = ""  # 设置站点标题
    site_footer = ""  # 设置站点的页脚
    menu_style = "accordion"  # 设置菜单折叠

xadmin.site.register(views.CommAdminView, GlobalSettings)

站点的全局配置

from .models import Book


class BookAdminModel(object):
    # 控制列表展示的字段 设置默认展示字段
    list_display = ['title', 'price', 'pub_date', 'publish', 'authors']  
    # 控制可以通过搜索框搜索的字段名称,xadmin使用的是模糊查询
    search_fields = ['id', 'title', 'price']  
    # 可以进行过滤操作的列,对于分类、性别、状态
    list_filter = ['id', 'title', 'price', 'publish']  
    # 排序,默认升序
    ordering = ['-price']  
    # 展示详情信息的字段
    show_detail_fields = ['title']  
    # 设置允许直接在展示页编辑的字段
    list_editable = ['price']  
    # 设置列表页的刷新频率
    refresh_times = [3, 6, 30, 60]  
    # 控制列表页导出数据的可选格式,设置None来禁用数据导出功能
    list_export = ['xls', 'csv', 'json']  
    # 控制是否显示书签功能,False表示关闭
    show_bookmarks = True  
    # 显示图表数据
    data_charts = {
        'price_total': {
            'title': '书籍单价曲线图',
            'x-field': 'id',
            'y-field': 'price',
            'order': ('id',),
        },
        # 可以有多个图表
        # 'price_total2': {
        #     'title': '书籍单价曲线图2',
        #     'x-field': 'id',
        #     'y-field': 'price',
        #     'order': ('id',),
        # }
    }
    # 控制菜单的图标(图标的设置可以参考boostrap的图标css名称)
    model_icon = 'fa fa-gift'
    # 设置编辑页中只读字段
    readonly_fields = ['title']
    # 设置在编辑页隐藏的字段
    exclude = ['pub_date']


xadmin.site.register(Book, BookAdminModel)

错误解决

#问题一
File "/data0/luffy/lib/python3.6/site-packages/xadmin/sites.py", line 7, in <module>
    from django.utils import six
ImportError: cannot import name 'six'
#解决:
1.pip3 install six
2.进入python3.6/site-packages
3.将six.py 复制到 django/utils即可

#问题二
  File "/data0/luffy/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 37, in <module>
    raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
#解决:
# if version < (1, 3, 13):
#     raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)


#问题三
  File "/data0/luffy/lib/python3.6/site-packages/xadmin/models.py", line 11, in <module>
    from django.utils.encoding import python_2_unicode_compatible, smart_text
ImportError: cannot import name 'python_2_unicode_compatible'
#解决:
vim /data0/luffy/lib/python3.6/site-packages/xadmin/models.py"
from django.utils.encoding import smart_text
from six import python_2_unicode_compatible

#问题四
  File "/data0/luffy/lib/python3.6/site-packages/xadmin/util.py", line 24, in <module>
    from django.contrib.staticfiles.templatetags.staticfiles import static
ModuleNotFoundError: No module named 'django.contrib.staticfiles.templatetags'
#解决:  
vim /data0/luffy/lib/python3.6/site-packages/xadmin/util.py
将
 from django.contrib.staticfiles.templatetags.staticfiles import static
改为
from django.templatetags.static import static
posted @ 2020-05-30 18:53  helloord  阅读(1658)  评论(0编辑  收藏  举报