【一】后台美化
pip install django-simpleui
INSTALLED_APPS = [
'simpleui',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'user',
'article'
]
【二】个性设置
【1】libs.CommonAdmin.py
from django.contrib import admin
class BaseModelAdmin(admin.ModelAdmin):
def display_created_time(self, obj):
return obj.create_time.strftime("%Y-%m-%d %H:%M:%S")
display_created_time.short_description = "创建时间"
def display_update_time(self, obj):
return obj.update_time.strftime("%Y-%m-%d %H:%M:%S")
display_update_time.short_description = "更新时间"
【2】user.admin.py
继承admin.ModelAdmin
【3】article.admin.py
from django.contrib import admin
# Register your models here.
from article.models import CategoryClass, CategoryTag, Comment, UpAndDown, Article, ArticleToTag
from libs.CommonAdmin import BaseModelAdmin
@admin.register(Article)
class ArticleAdmin(BaseModelAdmin):
def display_content(self, obj):
return obj.content[:20]
def display_desc(self, obj):
return obj.desc[:10]
display_content.short_description = '文章内容'
display_desc.short_description = '文章摘要'
list_display = ['id', 'title', 'display_desc', 'up_num', 'down_num', 'comment_num', 'display_content',
'display_created_time', 'display_update_time', 'blog', 'category']
@admin.register(CategoryClass)
class CategoryClassAdmin(admin.ModelAdmin):
ordering = ["id"]
list_display = ['id', 'name', 'blog']
@admin.register(CategoryTag)
class CategoryTagAdmin(admin.ModelAdmin):
ordering = ["id"]
list_display = ['id', 'name', 'blog']
@admin.register(ArticleToTag)
class CategoryTagAdmin(admin.ModelAdmin):
ordering = ["id"]
list_display = ['id', 'article', 'tag']
@admin.register(UpAndDown)
class UpAndDownAdmin(BaseModelAdmin):
ordering = ["id"]
list_display = ['id', 'user', 'article', 'up_or_down', 'display_created_time', 'display_update_time']
@admin.register(Comment)
class CommentAdmin(BaseModelAdmin):
ordering = ["id"]
list_display = ['id', 'user', 'article', 'content', 'is_deleted', 'parent', 'display_created_time','display_update_time']