Python教程

python-admin管理后台

本文主要是介绍python-admin管理后台,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

什么是admin管理后台

配置步骤


image
image
image
image

添加组

image

增加用户

image
image
image
image

对自定义表进行操作

注册自定义模型类

image
bookstore/admin.py

from django.contrib import admin
from .models import Book
# Register your models here.
# 
admin.site.register(Book)

![image](https://www.www.zyiz.net/i/l/?n=20&i=blog/1847830/202108/1847830-20210813091100380-
1343933388.png)

显示样式与__str__相同

image

模型管理器类

image
image
image

list_display

from django.contrib import admin
from .models import Book
# Register your models here.
# 

class BookMaage(admin.ModelAdmin):
    list_display=['id','title','pub','price']


admin.site.register(Book,BookMaage)

image

显示列名参数verbose_name

image

list_filter添加过滤器

from django.contrib import admin
from .models import Book
# Register your models here.
class BookMaage(admin.ModelAdmin):
    list_display=['id','title','pub','price']
   #过滤器 分类查询
    list_filter=['pub']
admin.site.register(Book,BookMaage)

image

search_fields

from django.contrib import admin
from .models import Book
# Register your models here.
# 

class BookMaage(admin.ModelAdmin):
    list_display=['id','title','pub','price']
    #添加搜索框[模糊搜索]
    search_fields=['title']

admin.site.register(Book,BookMaage)

image

添加可编辑字段 list_editable

from django.contrib import admin
from .models import Book
# Register your models here.
# 

class BookMaage(admin.ModelAdmin):
    list_display=['id','title','pub','price']
    
    #控制 list_display中的字段,可以链接到修改页 ,必须是list_display中
    list_display_links=['title']

    #过滤器 分类查询
    list_filter=['pub']

    #添加搜索框[模糊搜索]
    search_fields=['title']

    #添加可编辑字段
    list_editable=['price']

admin.site.register(Book,BookMaage)

image

这篇关于python-admin管理后台的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!