在home app下的models.py中写表模型
from django.db import models
# Create your models here.
# 编写一个BaseMosel表 继承了这张表就有了这张表的字段 相当于auth的AbstractUser
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 # 虚拟表,只用来做继承,不在数据库生成
class SlideShow(BaseModel):
# 图片地址,图片名,图片介绍,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='跳转链接') # /course/
info = models.TextField(verbose_name='详情') # 也可以用详情表
class Meta:
db_table = 'luffy_slide_show'
verbose_name_plural = '轮播图表'
def __str__(self):
return self.title
数据库迁移
python manage.py makemigrations
python manage.py migrate
轮播图接口
from django.shortcuts import render
# Create your views here.
from rest_framework.viewsets import GenericViewSet
from utils.list_mixinView import ListMixinView
from .models import SlideShow
from .serializer import SlideShowSer
# 重写了ListModelMixin 为了返回的格式统一
class SlideShowView(GenericViewSet, ListMixinView):
queryset = SlideShow.objects.all().filter(is_delete=False, is_show=True).order_by('orders')
serializer_class = SlideShowSer
重写ListModelMixin
from rest_framework.mixins import ListModelMixin
from utils.response import APIResponse
class ListMixinView(ListModelMixin):
def list(self, request, *args, **kwargs):
res = super().list(request, *args, **kwargs)
return APIResponse(code=1001, msg = 'ok', result=res.data)
序列化类
from rest_framework import serializers
from .models import SlideShow
class SlideShowSer(serializers.ModelSerializer):
class Meta:
model = SlideShow
fields = ['title', 'image', 'link']
使用路由分发,在home app中新建urls.py,直接copy总路由的代码
总路由
from django.contrib import admin
from django.urls import path, include
from django.views.static import serve
from django.conf import settings
from apps import home
urlpatterns = [
path('admin/', admin.site.urls),
path('media/' , serve,
kwargs={'document_root': settings.MEDIA_ROOT}),
# 路由分发
path('api/v1/home/', include('home.urls'))
]
home/urls.py
from rest_framework.routers import SimpleRouter
from home import views
router = SimpleRouter()
router.register('slide_show', views.SlideShowView, 'slide_show')
urlpatterns = [
]
urlpatterns.extend(router.urls)
pip install django-simpleui
注册
INSTALLED_APPS = [
'simpleui',
'django.contrib.admin',
'django.contrib.auth',
...
]
创建超级用户
python manage.py createsuperuser
存放公共的配置
在settins文件夹下新建common_settings.py文件
SLIDE_SHOW_COUNT = 3
在dev.py文件中导入
from .common_settings import SLIDE_SHOW_COUNT
使用时直接导配置文件的变量即可
from django.conf import settings
# 返回的轮播图个数
queryset = SlideShow.objects.all().filter(is_delete=False, is_show=True).order_by('orders')[:settings.SLIDE_SHOW_COUNT]