Django xadmin 后台管理平台

Django xadmin 后台管理平台


一、安装

xadmin是django的一个第三方的管理后台实现,它的功能比自带的admin功能更加强大。
https://github.com/sshwsfc/xadmin

1
pip install git+git://github.com/sshwsfc/xadmin.git

创建blogadmin project 和 blog app

1
2
3
4
(venv) Tony2018deMacBook-Pro:Django binbiwan$ pwd
/Users/binbiwan/Desktop/tony/study/python/project/devops/data/Django
(venv) Tony2018deMacBook-Pro:Django binbiwan$ django-admin startproject blogxadmin
(venv) Tony2018deMacBook-Pro:Django binbiwan$ python manage.py startapp blog

修改配置

blogxadmin/settings.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
INSTALLED_APPS = [
...
'xadmin',
'crispy_forms',
'reversion',
'blog',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'USER': 'root',
'PASSWORD': 'Cisco@hgh01',
'HOST': '10.224.172.80',
'PORT': '3306',
}
}
TIME_ZONE = 'Asia/Shanghai'

blogxadmin/urls.py

1
2
3
4
5
6
7
8
9
10
11
12
from django.conf.urls import include, url
from django.contrib import admin
import xadmin
from xadmin.plugins import xversion
xadmin.autodiscover()
xversion.register_models()
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^xadmin/', include(xadmin.site.urls), name='xadmin'),
]

blog/models.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=50)
content = models.TextField()
created_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.title

blog/admin.py

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
from __future__ import unicode_literals
from django.contrib import admin
# Register your models here.
import xadmin
from .models import Post
class PostAdmin(object):
search_fields = ('id', 'title', 'content')
fields = ('content', 'title')
list_display = ('preview', 'title', 'created_time')
list_display_links = ('title', )
ordering = ('-update_time', )
list_per_page = 15
def preview(self, obj):
return '''
<span><a href="/%s" target="_blank">preview</a></span>
''' % (obj.id, )
preview.short_description = 'Operations'
preview.allow_tags = True
# fields above just like django admin
xadmin.site.register(Post, PostAdmin)

生成数据库表

1
2
3
python manage.py makemigrations blog
python manage.py migrate

生成管理员用户

1
python manage.py createsuperuser

启动运行

1
python manage.py runserver

此处输入图片的描述

此处输入图片的描述