django中也有自带的admin后台管理系统,但是该管理系统较为简陋,完全无法满足日常维护的使用需求,所以通过xadmin来替代自带的admin后台管理系统。
由于xadmin项目官方已经停止维护,所以存在一些bug,因此我们将其源码下载下来,作为扩展库来使用,在使用过程中出现的问题自己来修改源码修复(下载地址)。
下载源码zip包到本地并解压,其中几个目录可以使用:
demo_app
:此目录中含有一个demo项目,可以作为开发参考;xadmin
:此目录为源码目录,可以直接拷贝到项目的扩展app目录使用,具体使用方法后面详细介绍;requirements.txt
:此文件为xadmin依赖的三方库环境文件。 进入demo_app
目录,并打开cmd窗口,安装虚拟环境(安装虚拟环境具体步骤可以参考Django开发总结-(二)Virtualenv虚拟环境搭建)
Admin@SUPERWONG D:\Projects\xadmin-django2\demo_app
# dir
驱动器 D 中的卷是 工作
卷的序列号是 3360-D81E
D:\Projects\xadmin-django2\demo_app 的目录
2019/03/06 21:41 <DIR> .
2019/03/06 21:41 <DIR> ..
2019/03/06 21:03 <DIR> app
2018/10/31 14:49 43,394 data.json
2019/03/06 21:12 278,528 db.sqlite3
2019/03/06 21:03 <DIR> demo
2018/10/31 14:49 373 manage.py
3 个文件 322,295 字节
4 个目录 271,286,386,688 可用字节
Admin@SUPERWONG D:\Projects\xadmin-django2\demo_app
# virtualenv --no-site-package -p python venv
Running virtualenv with interpreter C:\Python\Python36\python.exe
Using base prefix 'C:\\Python\\Python36'
New python executable in D:\Projects\xadmin-django2\demo_app\venv\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
Admin@SUPERWONG D:\Projects\xadmin-django2\demo_app
# dir
驱动器 D 中的卷是 工作
卷的序列号是 3360-D81E
D:\Projects\xadmin-django2\demo_app 的目录
2019/03/06 21:45 <DIR> .
2019/03/06 21:45 <DIR> ..
2019/03/06 21:03 <DIR> app
2018/10/31 14:49 43,394 data.json
2019/03/06 21:12 278,528 db.sqlite3
2019/03/06 21:03 <DIR> demo
2018/10/31 14:49 373 manage.py
2019/03/06 21:45 <DIR> venv
3 个文件 322,295 字节
5 个目录 271,258,865,664 可用字节
Admin@SUPERWONG D:\Projects\xadmin-django2\demo_app
#
进入虚拟环境,并在虚拟环境中安装xadmin依赖三方库
Admin@SUPERWONG D:\Projects\xadmin-django2\demo_app
# .\venv\Scripts\activate
(venv) Admin@SUPERWONG D:\Projects\xadmin-django2\demo_app
# pip install -r ..\requirements.txt
初始化数据库,创建超级管理员,启动服务
(venv) Admin@SUPERWONG D:\Projects\xadmin-django2\demo_app
# python manage.py migrate
Operations to perform:
Apply all migrations: admin, app, auth, contenttypes, reversion, sessions, 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 admin.0003_logentry_add_action_flag_choices... 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 app.0001_initial... OK
Applying app.0002_idc_groups... OK
Applying app.0003_host_administrator... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying reversion.0001_squashed_0004_auto_20160611_1202... OK
Applying sessions.0001_initial... OK
Applying xadmin.0001_initial... OK
Applying xadmin.0002_log... OK
Applying xadmin.0003_auto_20160715_0100... OK
(venv) Admin@SUPERWONG D:\Projects\xadmin-django2\demo_app
# python manage.py createsuperuser
用户名 (leave blank to use 'admin'): admin
电子邮件地址: 1@1.com
Password:
Password (again):
这个密码太常见了。
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
(venv) Admin@SUPERWONG D:\Projects\xadmin-django2\demo_app
# python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
March 06, 2019 - 22:03:58
Django version 2.1.7, using settings 'demo.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
登陆xadmin系统,查看其demo项目
将xadmin源码拷贝到项目扩展app目录,并将环境依赖文件requirements.txt拷贝到项目根目录下
转载app,包括xadmin
、crispy_forms
、reversion
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# -----xadmin相关app装载-------- #
'xadmin',
'crispy_forms',
'reversion',
# ------------------------------ #
'apps.users'
]
配置djangoCloudMusic/url.py
,注册xadmin相关路由,并屏蔽自带admin路由配置
from django.contrib import admin
from django.urls import path
import xadmin
from xadmin.plugins import xversion
# 装载xadmin
xadmin.autodiscover()
# version模块自动注册需要版本控制的 Model
xversion.register_models()
urlpatterns = [
# path('admin/', admin.site.urls),
path('xadmin/', xadmin.site.urls)
]
进入虚拟机(虚拟机搭建步骤可参考Django开发总结-(一)Vagrant虚拟环境搭建)中,通过pip install -r requirements.txt
命令安装依赖三方库
(venv) [vagrant@localhost djangoCloudMusic]$ ls
apps db.sqlite3 djangoCloudMusic ext_apps manage.py requirements.txt venv
(venv) [vagrant@localhost djangoCloudMusic]$ pip install -r requirements.txt
通过python manage.py migrate
命令同步数据库
(venv) [vagrant@localhost djangoCloudMusic]$ python manage.py migrate
/home/vagrant/src/djangoCloudMusic/venv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
Operations to perform:
Apply all migrations: admin, auth, contenttypes, reversion, sessions, users, xadmin
Running migrations:
Applying reversion.0001_squashed_0004_auto_20160611_1202... OK
Applying xadmin.0001_initial... OK
Applying xadmin.0002_log... OK
Applying xadmin.0003_auto_20160715_0100... OK
通过python manage.py runserver
启动服务
(venv) [vagrant@localhost djangoCloudMusic]$ python manage.py runserver 10.0.2.15:8000
/home/vagrant/src/djangoCloudMusic/venv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
/home/vagrant/src/djangoCloudMusic/venv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: .
""" )
Performing system checks...
System check identified no issues (0 silenced).
March 06, 2019 - 22:39:37
Django version 2.1.7, using settings 'djangoCloudMusic.settings'
Starting development server at http://10.0.2.15:8000/
Quit the server with CONTROL-C.
浏览器访问xadmin服务
在apps/users
目录下创建adminx.py
配置文件,进行系统级配置、用户app相关xadmin配置
配置参数并通过xadmin.sites.register
修饰器进行注册,配置参数如下:
BaseSetting
:基础设置
enable_themes
:是否显示主题选择;use_bootswatch
:是否使用扩展主题。GlobalSettings
:全局设置
site_title
:左上角的项目名称;site_footer
:页脚的copyright;global_search_models
:使用全局搜索的model;global_models_icon
:全局搜索model的图标;menu_style
:菜单样式,默认为展开模式,accordion为折叠模式。from django.contrib.auth import get_user_model
import xadmin
from xadmin import views
User = get_user_model()
@xadmin.sites.register(views.BaseAdminView)
class BaseSetting(object):
enable_themes = True
use_bootswatch = True
@xadmin.sites.register(views.CommAdminView)
class GlobalSettings(object):
site_title = "DjangoCloudMusic"
site_footer = "2019 Super.Wong"
global_search_models = [User]
global_models_icon = {
User: "fa fa-user"
}
menu_style = 'default' # 'accordion'
由于我们对django自带的用户信息model进行了扩展,对于xadmin配置也需要进行调整,以此model的配置也可以反映出其他model的配置方式。
首先找到xadmin中的用户配置源码(ext_apps/xadmin/plugins/auth.py
),并进行修改,将原注册代码屏蔽。
修改apps/users/adminx.py
文件,重新配置并通过@xadmin.sites.register
装饰器注册,修改内容如下:
list_display
:设置展示列表中显示的列;list_filter
:设置展示列表上方的过滤器的列,一般作为过滤器的列为可选择的CharField字段,日期时间字段、关系型字段(外键、多对多等)不作为过滤器使用;search_fields
:设置展示列表的查询列,对于关系型字段可以通过__
方式设置关系表中的字段作为查询条件;ordering
:排序字段get_form_layout
:此函数用于定义明细展示中的区域划分,将新增的几个字段划分到Personal info
用户信息区域内。from django.contrib.auth import get_user_model
from django.utils.translation import ugettext as _
import xadmin
from xadmin.plugins.auth import UserAdmin
from xadmin.layout import Fieldset, Main, Side, Row
User = get_user_model()
@xadmin.sites.register(User)
class UserProfileAdmin(UserAdmin):
list_display = ('username', 'first_name', 'last_name', 'nickname', 'mobile', 'is_superuser', 'is_active',
'is_staff', 'date_joined', 'last_login')
search_fields = ('username', 'first_name', 'last_name', 'nickname')
ordering = ('-date_joined', 'username',)
def get_form_layout(self):
if self.org_obj:
self.form_layout = (
Main(
Fieldset('',
'username', 'password',
css_class='unsort no_title'
),
Fieldset(_('Personal info'),
Row('first_name', 'last_name'),
Row('email', 'mobile'),
Row('nickname', 'birthday'),
'gender',
),
Fieldset(_('Permissions'),
'groups', 'user_permissions'
),
Fieldset(_('Important dates'),
'last_login', 'date_joined'
),
),
Side(
Fieldset(_('Status'),
'is_active', 'is_staff', 'is_superuser',
),
)
)
return super(UserAdmin, self).get_form_layout()