五、开发后台管理(一)
第四节中迁移了数据库并且创建了后台管理员,登录后台发现三个应用还没有注册进入后台,这一节记录了后台开发,到后面我们会使用xadmin来替换比较简陋的admin后台
先编辑blog/admin.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from .models import Post, Category, Tag
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = [
"title",
"status",
"category",
"author",
"created_time",
]
list_filter = (
"author",
)
date_hierarchy = "created_time"
# 编辑页面
fields = (
("title", 'category'),
"tag",
"author",
"desc",
"status",
"content",
)
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = [
"name",
"status",
"is_nav",
"author",
"created_time",
]
fields = (
"name",
"status",
"is_nav",
"author",
)
@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
list_display = [
"name",
"status",
"author",
"created_time",
]
fields = (
"name",
"status",
"author",
)
进入后台,我们先新增一篇文章,可以看到标签和分类是object,这里需要在models.py中增加定制类
class Tag(models.Model):
............
def __unicode__(self):
return self.name
class Category(models.Model):
................
def __unicode__(self):
return self.name
class Post(models.Model):
.................
def __unicode__(self):
return self.name
config和comment应用也是这样开发后台,具体代码可以去github查看
网友评论