Django后台管理定制admin

---恢复内容开始---

结构如下

mysite
|
|--monitor
|    |
|    |--views.py
|         |--urls.py
|    |--admin.py
|       |--models.py
|--mysite(默认创建)
|    |
|    |--settings.py
|       |--urls.py
|--manage.py
|
|--db.sqlite3

首先定义models.py,生成数据库的表结构

from django.db import models
from django.utils.html import  format_html
# Create your models here.
class  UserInfo(models.Model):
    Username = models.CharField(max_length=12,help_text="Ple input your name")
    Password = models.CharField(max_length=12,blank=True,default="kkk")
    Age = models.IntegerField()
    def __str__(self):
        return  "< %s >"%(self.Username)
class Author(models.Model):
    first_name = models.CharField(max_length=32)
    last_name = models.CharField(max_length=5)
    email = models.EmailField()
    def __str__(self):
        return  "<%s>"%(self.first_name)
class  Publisher(models.Model):
    name = models.CharField(max_length=32,unique=True)
    address = models.CharField(max_length=128)
    city = models.CharField(max_length=32)
    web_site = models.URLField()
    date = models.DateField()
    def __str__(self):
        return "<%s>"%(self.name)
class Books(models.Model):
    name = models.CharField(max_length=32)
    authors = models.ManyToManyField(Author)
    publishers = models.ForeignKey(Publisher,on_delete=models.CASCADE)
    publish_date = models.DateField()
    status_choice = (('published', u'已出版'),  #定义一个可选择的框,前提下面必须定义一个默认值
              ('producing', u'待出版'),
              ('forbidden', u'禁书'),
              )
    status = models.CharField(choices=status_choice,max_length=32,default='producing')
    def __str__(self):
        return "<%s>"%(self.name)
    def color_set(self):
        if self.status == "published":
            f_html = format_html('<span style="padding:2px;background-color:green;color:white">已出版</span>')
        elif self.status == "producing":
            f_html = format_html('<span style="padding:2px;background-color:pink;color:white">待出版</span>')
        else:
            f_html =format_html('<span style="padding:2px;background-color:yellow;color:red">禁书</span>')
        return f_html
    color_set.short_description = "status"
View Code

然后进入mysite目录执行命令来创建数据库表

python  manage.py  makemigrations
python  manage.py   migrate

打开admin开始定制页面

from django.contrib import admin

# Register your models here.
from . import  models

def make_forbiddend(modeladmin,request,queryset):
    queryset.update(status='forbidden')
make_forbiddend.short_description = "设置所选为禁书"

def  make_producding(modeladmin,request,queryset):
    queryset.update(status='producing')
make_producding.short_description = "设置所选为待出版"

def make_published(modeladmin,request,queryset):
    queryset.update(status='published')
make_published.short_description = "设置所选为已出版"

class BookAdmin(admin.ModelAdmin):  #定制book方法
    list_display = ('id',"name",'publishers','publish_date','color_set','status')  #指定要显示的子项
    list_editable = ("status",)       #指定哪些选项可以编辑
    filter_horizontal = ('authors',)   #只对多对多关系选项
    actions = ( make_producding,make_forbiddend,make_published)   #在action里所要列出的动作,在上面定义func
class authorAdmin(admin.ModelAdmin):
    list_display = ("first_name",'last_name','email')
class publishAdmin(admin.ModelAdmin):
    list_display = ('id','name','address','city','web_site','date')
    search_fields = ("name",'city')  #定义一个搜索框,索引以name或者city
    list_filter = ('name','city','web_site')   #在右边显示全部过滤
    list_editable = ('address','city','date')  #可以在页面直接修改数据
    list_per_page = 5                             #每页显示几行
admin.site.register(models.UserInfo)    #把指定的方法注册给admin后台可管理
admin.site.register(models.Author,authorAdmin)
admin.site.register(models.Publisher,publishAdmin)
admin.site.register(models.Books,BookAdmin)    #把定制好的方法写后面生效
View Code

启动服务python manage.py runserver

访问后面127.0.0.1:8000/admin

posted on 2018-06-12 09:41  世马  阅读(195)  评论(0编辑  收藏  举报