Django-Xadmin使用
我们使用xadmin作为Django框架的后端管理平台
1 安装xadmin
下载xadmin https://github.com/nocmt/Xadmin1.11.x.git
将主目录的xadmin拷贝到extra_apps目录下
加入依赖包,(venv) $ pip freeze > xadmin_requirements.txt (忽略)
vim xadmin_requirements.txt , 加入如下信息:
six
future
httplib2
django-reversion
django-formtools
django-crispy-forms
django-import-export
- 1
- 2
- 3
- 4
- 5
- 6
- 7
(venv) $ pip install -r xadmin_requirements.txt
在django中settings.py的注册表INSTALLED_APPS中加入:
'xadmin',
'crispy_forms',
- 1
- 2
python manage.py makemigrations
python manage.py migrate
在主工程的urls.py 中改动:
#from django.contrib import admin
import xadmin
urlpatterns = [
#url(r'^admin/', admin.site.urls),
url(r'^xadmin/', xadmin.site.urls),
- 1
- 2
- 3
- 4
- 5
- 6
在应用app的admin.py中改动
import xadmin
# Register your models here.
#admin.site.register(Tag)
xadmin.site.register(Tag)
xadmin.site.register(Art)
- 1
- 2
- 3
- 4
- 5
创建超级用户,创建迁移文件,迁移数据到db中,创建超级用户
python manage.py createsuperuser
账号
启动服务,登陆xadmin
2 配置完善xadmin
(1)页面中文展示:
settings.py中配置
#LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-hans'
#TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'
USE_TZ = False
- 1
- 2
- 3
- 4
- 5
- 6
- 7
(2)在models层的实体类中加入verbose_name
#文章标签
class Tag(models.Model):
t_name = models.CharField(max_length=50, verbose_name="标签名")
t_info = models.CharField(max_length=200, verbose_name="标签描述")
t_addtime = models.DateField(default=timezone.now, db_index=True, verbose_name="添加时间")
class Meta:
db_table = "tag"
verbose_name = "标签"
verbose_name_plural = verbose_name
ordering = ["-t_addtime"]
def __str__(self):
return self.t_name
class Art(models.Model):
a_title = models.CharField(max_length=255, verbose_name="文章标题")
a_info = models.CharField(max_length=500, verbose_name="备注")
a_content = models.TextField(verbose_name="文章内容")
a_img = models.ImageField(null=True, blank=True,
upload_to="uploads", verbose_name="文章图片")
a_addtime = models.DateTimeField(default=timezone.now, db_index=True, verbose_name="创建时间")
a_updatetime = models.DateTimeField(default=timezone.now, verbose_name="更新时间")
a_tag = models.ForeignKey(Tag) #a_tag_id
#python2.7采用的方式
def __unicode__(self):
return self.a_title
#python3.x采用的方式
def __str__(self):
return self.a_title
class Meta:
ordering = ['-a_addtime']
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
加上属性字段对应的别名verbose_name, 字段中文展示
(3)在art/admin.py 中添加如下xadmin配置
#coding: utf-8
#from django.contrib import admin
# Register your models here.
from art.models import Tag, Art
import xadmin
from xadmin import views
class BaseSetting(object):
# 主题修改
enable_themes = True
use_bootswatch = True
class GlobalSettings(object):
#整体配置
site_title = '美文后台管理系统'
site_footer = '武汉市创业python项目'
menu_style = 'accordion' #菜单折叠
class TagAdmin(object):
#后台列表显示列
list_display = ['t_name', 't_info', 't_addtime']
# 后台列表查询条件
search_fields = ['t_name', 't_addtime']
# 后台列表通过时间查询
list_filter = ['t_name', 't_info', 't_addtime']
list_per_page = 10
class ArtAdmin(object):
# 后台列表显示列
list_display = ['a_title', 'a_info', 'a_content', 'a_img', 'a_addtime', 'a_updatetime']
# 后台列表查询条件
search_fields = ['a_title', 'a_info', 'a_content']
# 后台列表通过时间查询
list_filter = ['a_title', 'a_info', 'a_content', 'a_addtime']
list_per_page = 20
xadmin.site.register(views.CommAdminView, GlobalSettings)
xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(Tag, TagAdmin)
xadmin.site.register(Art, ArtAdmin)
#admin.site.register(Tag)
#admin.site.register(Art)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
(3)修改应用目录下的apps.py文件
from django.apps import AppConfig
class ArtConfig(AppConfig):
name = 'art'
verbose_name = '美文信息管理' #应用art对应的中文名称
- 1
- 2
- 3
- 4
- 5
- 6
(4)在应用的init.py文件中加入
default_app_config = "art.apps.ArtConfig"
- 1
(5)启动web服务,在后端访问