Contents [hide]
运行环境
预先安装好虚拟环境,搭建Python3.5环境:
mkvirtualenv django-docs -p python3
需要安装:
python3.5.3 + Django2.0.6 + xadmin
安装xadmin
两种安装方式:
方式1
pip install git+git://github.com/sshwsfc/xadmin.git@django2
官方包安装后项目启动显示django-formtools
版本有问题,卸载后安装新版本即可:
pip uninstall django-formtools
pip install django-formtools==2.1
方式2:安装修复包
pip install git+git@github.com:xiaofeng283-t/xadmin.git@django2
创建Django项目
django-admin startproject djangodocs
配置settings.py
INSTALLED_APPS = [
'xadmin',
'crispy_forms',
'reversion',
]
配置urls.py
import xadmin
urlpatterns = [
path('xadmin/', xadmin.site.urls),
]
启动项目
python manage.py runserver
启动成功!
打开网址:http://127.0.0.1:8000
同步数据表
安装mysql包:
pip install pymysql
配置启动文件__init__.py
:
import pymysql
pymysql.install_as_MySQLdb()
配置settings.py数据库信息:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DatabaseName', # 数据库
'USER': ‘USERNAME’, # 用户名
'PASSWORD': ‘PWD’, # 密码
'HOST': '127.0.0.1',
}
}
同步数据表:
python manage.py makemigrations
python manage.py migrate
成功创建表
创建管理员
$ python manage.py createsuperuser
Username (leave blank to use 'jervon'): admin
Email address: jervon@foxmail.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is too common.
Password:
Password (again):
Superuser created successfully.
密码长度至少8位
进入xadmin
重新启动项目,打开网址:http://127.0.0.1:8000/xadmin/
配置时区/字体
settings.py
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False
定制
用户模块
有时候xadmin默认字段无法满足需求,就需要定制;
新建apps
目录,存放创建的模块,创建users
模块:
python startapp users
移动users
模块到apps
目录,修改models.py
:
from django.contrib.auth.models import AbstractUser
class UserProfile(AbstractUser):
nick_name = models.CharField(max_length=50, verbose_name=u'昵称', blank=True, null=True)
birthday = models.DateField(verbose_name=u'生日', blank=True, null=True)
address = models.CharField(max_length=100, verbose_name=u'地址', blank=True, null=True)
age = models.IntegerField(verbose_name=u'年龄', blank=True, null=True)
gender = models.CharField(max_length=10, choices=(('Female', u'女'), ('Male', '男')), verbose_name=u'性别', default='Male')
image = models.ImageField(upload_to='images/%Y/%m', verbose_name=u'头像')
class Meta:
verbose_name = u'用户信息'
verbose_name_plural = verbose_name
def __unicode__(self):
return self.username
创建adminx.py
:
import xadmin
from .models import UserProfile
class UserProfileAdmin(object):
pass
xadmin.site.register(UserProfile, UserProfileAdmin)
配置settings.py
:
import sys
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
同步数据结构:
python manage.py makemigrations users
报错:
xadmin.sites.AlreadyRegistered: The model UserProfile is already registered
解决办法见文末:问题1
继续同步数据表:
python manage.py makemigrations
python manage.py migrate
报错:
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration reversion.0001_squashed_0004_auto_20160611_1202 is applied before its dependency users.0001_initial on database 'default'.
解决办法见文末:问题2
后台菜单
这个功能要实现的修改左侧菜单分类
users
,没有整合成功,目前不影响使用,先一并记录
apps/users/apps.py
from django.apps import AppConfig
class UserConfig(AppConfig):
name = 'user'
verbose_name = u'用户操作'
增加verbose_name = u'用户操作'
apps/users/init.py
default_app_config='apps.users.apps.UserConfig'
运行项目后报错
django.core.exceptions.ImproperlyConfigured: Cannot import 'user'. Check that 'apps.users.apps.UserConfig.name' is correct.
暂时没解决,解决后补充!
项目结构目录
.
├── config
└── djangodocs
├── apps
│ └── users
│ ├── __init__.py
│ ├── admin.py
│ ├── adminx.py
│ ├── apps.py
│ ├── migrations
│ │ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── djangodocs
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── static
├── admin
└── xadmin
问题
问题1:xadmin.sites.AlreadyRegistered: The model UserProfile is already registered
完整错误提示:
xadmin.sites.AlreadyRegistered: The model UserProfile is already registered
解决:
修改adminx.py
# 先注销模块,再注册
xadmin.site.unregister(UserProfile)
xadmin.site.register(UserProfile, UserProfileAdmin)
问题2:django.db.migrations.exceptions.InconsistentMigrationHistory
完整错误提示:
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration reversion.0001_squashed_0004_auto_20160611_1202 is applied before its dependency users.0001_initial on database 'default'.
原因:Django中有一个原生的User模型类,admin的模型依赖这个模型类,由于前面一个应用中的模型类User继承了AbstractUser类,所以提示这个错误。
解决方案:
删除数据库中 除了auth_user的其他表,然后重新来一次。
Operations to perform:
Apply all migrations: admin, auth, contenttypes, reversion, sessions, users, xadmin
Running migrations:
Applying contenttypes.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0001_initial... 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 auth.0009_alter_user_last_name_max_length... OK
Applying users.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... 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
重新创建超级管理员:
python manage.py createsuperuser
参考
(完)