django-xadmin使用基础环境为:
PS:如下环境如需升级python则先升级python,然后安装django
python3.6.4安装: http://www.cnblogs.com/cjsblogs/p/8117272.html
django1.9安装: http://www.cnblogs.com/cjsblogs/p/8023014.html
mysql-5.7.17安装: http://www.cnblogs.com/cjsblogs/p/8116782.html
xadmin安装: pip install xadmin
xadmin使用:
django-admin startproject devops
cd devops
django-admin startapp myapp
vim devops/settings.py:
#页面显示中文
LANGUAGE_CODE = 'zh-hans'
#TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'
#注册xadmin以及应用
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'xadmin',
'crispy_forms',
'reversion',
'tim',
]
#链接数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'devops',
'USER': 'root',
'PASSWORD': '***',
'HOST': '*.*.*.*',
'PORT': '',
}
}
vim devops/urls.py:
from django.conf.urls import include, url
from xadmin.plugins import xversion
import xadmin
xversion.register_models()
xadmin.autodiscover()
urlpatterns = [
url(r'xadmin/', include(xadmin.site.urls)),
]
至此,django-xadmin就初步搭建完成了:
初始化数据库:
python manage.py makemigrations
python manage.py migrate
启动应用:
python manage.py runserver IP:PORT &
创建用户,用来登陆管理网站:
python manage.py createsuperuser
输入想要使用的用户名:
Username (leave blank to use 'administrator'):
输入email:
Email address: (在这里输入你的自己的邮箱帐号)
输入密码,需要输入两次,并且输入密码时不会显示出来:
Password:
Password (again):
当两次密码都相同的时候,就会提示超级帐号创建成功。
Superuser created successfully
以下为xadmin的myapp进一步使用:
下载:https://github.com/sshwsfc/xadmin
解压xadmin-master.zip文,
unzip xadmin-master.zip
将xadmin-master文件中xadmin目录复制到自己的django项目下
cd _xadmin-master_
cp -a xadmin ../devops/
vim myapp/models.py
#coding:utf-
from __future__ import unicode_literals
from django.db import models
class dev(models.Model):
servername = models.CharField(max_length=, default='',verbose_name='系统')
appname = models.CharField(max_length=, default='',verbose_name='应用名称')
domainname = models.CharField(max_length=,default='', verbose_name='域名')
ip = models.CharField(max_length=,default='',verbose_name='宿主机IP')
port = models.CharField(max_length=,default='',verbose_name='端口')
class Meta:
verbose\_name = "DEV"
verbose\_name\_plural = verbose\_name
def \_\_str\_\_(self):
return self.servername
vim myapp/adminx.py
#coding:utf-
import xadmin
from .models import dev
from xadmin import views
class BaseSetting(object):
enable_themes = True
use_bootswatch = True
xadmin.site.register(views.BaseAdminView, BaseSetting)
class GlobalSetting(object):
site_title = "系统"
site_footer = "http://*.*.*.*/xadmin/"
menu_style = "accordion"
xadmin.site.register(views.CommAdminView, GlobalSetting)
class devAdmin(object):
list_display = ["servername","appname", "domainname", "ip", "port"]
search_fields = ["servername","appname"]
list_filter = ["servername","appname"]
xadmin.site.register(dev, devAdmin)
vim myapp/apps.py
# -*- coding: utf- -*-
from django.apps import AppConfig
class MyappConfig(AppConfig):
name = 'myapp'
verbose_name = u'用户信息'
vim myapp/__init__.py
default_app_config = 'myapp.apps.MyappConfig'
python manage.py makemigrations
python manage.py migrate
重启应用即可
手机扫一扫
移动阅读更方便
你可能感兴趣的文章