其他分享
首页 > 其他分享> > 后台主页轮播图接口并引入simpleui

后台主页轮播图接口并引入simpleui

作者:互联网

utils/model.py/BaseModel

  存放公共字段的

from django.db import models
# 5个公共字段
class BaseModel(models.Model):
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='最后更新时间')
    is_delete = models.BooleanField(default=False, verbose_name='是否删除')
    is_show = models.BooleanField(default=True, verbose_name='是否上架')
    orders = models.IntegerField(verbose_name='优先级')
    class Meta:
        abstract = True  # 表示它是虚拟的,不在数据库中生成表,它只用来做继承

 Banner表

  前提先创建home这个app

from django.db import models

# Create your models here.
# 轮播图接口---》轮播图表
from utils.model import BaseModel
class Banner(BaseModel):
    # 顺序,插入时间, 是否显示,是否删除。。。----》后期写课程的表也会用到这些字段--->仿AbstractUser,写一个基表,以后继承这个表
    # 继承过来,只需要写自有字段即可:title,image,info,link
    title = models.CharField(max_length=16, unique=True, verbose_name='名称')
    image = models.ImageField(upload_to='banner', verbose_name='图片')
    link = models.CharField(max_length=64, verbose_name='跳转链接') # 在前端点击图片,会跳转到某个地址
    info = models.TextField(verbose_name='详情')  # 也可以用详情表,宽高出处
    class Meta:
        db_table = 'luffy_banner'
        verbose_name_plural = '轮播图表'

    def __str__(self):
        return self.title

迁移数据

  app要注册

# python manage.py makemigrations  ---》如果没有变化,是app没注册
# python manage.py migrate

创建超级用户

# python manage.py createsuperuser  --->创建个用户

引入simpleui

  下载

pip install django-simpleui

  注册app

INSTALLED_APPS = [
      'simpleui',
      ...
  ]

  在admin中写

from django.contrib import admin
from .models import Banner

@admin.register(Banner)
class BannerAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'link','is_show', 'is_delete')

    # 增加自定义按钮
    actions = ['make_copy']
    def make_copy(self, request, queryset):
        # 选中一些数据,点击 【自定义按钮】  触发方法执行,传入你选中 queryset
        # 保存,删除
        print(queryset)
    make_copy.short_description = '自定义按钮'

 

 轮播图接口

  想要返回的数据格式

{code:100,msg:成功,result:[{img:地址,link:跳转地址,orders:顺序,title:名字},{img:地址,link:跳转地址,orders:顺序,title:名字}]}

  home路由

from django.urls import path, include
from rest_framework.routers import SimpleRouter
from .views import BannerView

router = SimpleRouter()
router.register('banner', BannerView, 'banner')
urlpatterns = [
    path('', include(router.urls)),

]

  视图类

from .models import Banner
from .serializer import BannerSerializer
from utils.response import APIResponse
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin
class BannerView(GenericViewSet,ListModelMixin):
    # 获取所有接口-list,自动生成路由
    queryset = Banner.objects.filter(is_delete=False,is_show=True).order_by('orders')
    serializer_class =BannerSerializer

    def list(self, request, *args, **kwargs): # 重写list
        res=super().list(request, *args, **kwargs)
        return APIResponse(result=res.data)

  序列化类

from rest_framework import serializers
from .models import Banner

class BannerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Banner
        fields = ['title', 'image', 'link', 'orders']

  总路由

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/v1/home/', include('home.urls')), # http://127.0.0.1:8000/api/v1/home/banner/
]

 

标签:verbose,轮播,主页,models,Banner,import,simpleui,class,name
来源: https://www.cnblogs.com/zhaoyuanshi/p/16172687.html