xadmin的安装与使用

xadmin是什么?Xadmin 采用Python语言编写,框架层基于最成熟的Web框架 Django,是一个基于Django的admin的一个后台模板框架。

安装

# 方式一
pip install xadmin

# 方式二
# 下载xadmin源码包:https://github.com/sshwsfc/xadmin
pip install xadmin-master.zip

在INSTALLED_APPS注册

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'xadmin',
    'crispy_forms',
]

修改路由

# 这里我们替换掉admin路由
import xadmin

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

这样我们就成功用xadmin替换了admin,可以看一下页面对比(上:原始admin)

xadmin的安装与使用

xadmin的安装与使用

注册Model到站点

# 在app下新建一个adminx.py文件
import xadmin

class ModelAdmin(object):
    pass
    
xadmin.site.register(Model类,ModelAdmin管理类)

这样我们就成功将模型类添加至了后台管理站点,接下来我们通过一个列子让大家看到效果:

xadmin的安装与使用
其中adminx文件:

# -*- coding:utf-8 -*-
"""
Create by Jim on2019/01/16
"""
import xadmin

from users.models import People


class PeopleAdmin(object):
    pass

xadmin.site.register(People, PeopleAdmin)

接下来我们通过manage.py@xadmintest控制台进行数据迁移:

manage.py@xadmintest > makemigrations
"C:\PyCharm 2017.3.7\bin\runnerw.exe" G:\python\python.exe "C:\PyCharm 2017.3.7\helpers\pycharm\django_manage.py" makemigrations "G:/pycharm project/xadmintest"
Migrations for 'users':
  users\migrations\0001_initial.py
    - Create model People

Following files were affected 
 G:\pycharm project\xadmintest\users\migrations\0001_initial.py
Process finished with exit code 0
manage.py@xadmintest > migrate
"C:\PyCharm 2017.3.7\bin\runnerw.exe" G:\python\python.exe "C:\PyCharm 2017.3.7\helpers\pycharm\django_manage.py" migrate "G:/pycharm project/xadmintest"
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, users, xadmin
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK
  Applying users.0001_initial... OK
  Applying xadmin.0001_initial... OK
  Applying xadmin.0002_log... OK
  Applying xadmin.0003_auto_20160715_0100... OK

Following files were affected 
 G:\pycharm project\xadmintest\db.sqlite3

然后就是创建管理员账号了:这里我设置用户名为admin,密码为adminjim,邮箱为空

manage.py@xadmintest > createsuperuser
"C:\PyCharm 2017.3.7\bin\runnerw.exe" G:\python\python.exe "C:\PyCharm 2017.3.7\helpers\pycharm\django_manage.py" createsuperuser "G:/pycharm project/xadmintest"
Username (leave blank to use 'administrator'):  admin
Email address:  
Warning: Password input may be echoed.
Password:  adminjim
Warning: Password input may be echoed.
Password (again):  adminjim
Superuser created successfully.

登录账号:

xadmin的安装与使用
这里我们发现我们添加的model就显示出来了,接下来我们可以点击模型类,然后添加数据

xadmin的安装与使用
添加成功我们点击这个数据

xadmin的安装与使用
这里我们可以修改和删除,是不是很方便,当然我们的系统显示还有很多需要修改,如:主题,左上角的文字,页面底部文字,列表显示英文,搜索功能等等,我们都可以做添加修改,大家回想一下我们前面在注册的时候是不是还写了一个多余模型管理类呢,那么他就派上用场了,它就可以用来操作这些。由于东西比较多,这里我就不多说了,期待我的下一篇文章吧。

相关推荐