后台主页模块设计
一、创建app home并注册
1. apps下创建home项目
前提:在 luffy 虚拟环境下
# 1.终端从项目根目录进入apps目录 cd luffyapi & cd apps # 2.创建app python ../../manage.py startapp home
2.dev.py中注册项目
INSTALLED_APPS = [ # ... 'rest_framework', 'home', ]
二、路由分发
1. 主路由:luffyapi/urls.py
urlpatterns = [ path('api/v1/home/', include('home.urls')), ]
2. 子路由:在home的app中新建url.py
from rest_framework.routers import SimpleRouter from . import views router = SimpleRouter() # 访问 http://127.0.0.1:8000/api/v1/home/banner ---->get 请求就可以查询所有轮播图 router.register('banner', views.BannerView, 'banner') urlpatterns = [ ] urlpatterns += router.urls
三、Banner数据表model设计
# 分析原型图,首页要写的接口 - 轮播图接口 - 推荐课程接口(暂时没写) - 推荐老师(暂时没有) - 学员评论(暂时没有)
第一步:utils目录下新建一个py文件——common_model,写一个基表
- 以后其他模型表继承它,只用作继承,不真正创建表
# common_model
from django.db import models 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 # 只用来继承,不用来在数据库中创建表
第二步:在home 的app的models.py中编写轮播图表 Banner
from django.db import models from utils.common_model import BaseModel # Create your models here. class Banner(BaseModel): # 哪些字段:真正图片地址,标题,跳转链接,图片介绍 # 是否删除(软删除),是否显示,优先级,创建时间,更新事件:公共字段 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
四、设计Banner数据接口
1. home/serializers.py
from rest_framework import serializers from .models import Banner class BannerSerializer(serializers.ModelSerializer): class Meta: model = Banner fields = ['title', 'image', 'link']
2. home/views.py
from rest_framework.viewsets import GenericViewSet from rest_framework.mixins import ListModelMixin from .models import Banner from .serializer import BannerSerializer from utils.common_response import APIResponse class BannerView(GenericViewSet,ListModelMixin): queryset = Banner.objects.filter(is_delete=False,is_show=True).order_by('orders') serializer_class = BannerSerializer
3. 视图函数优化
- 想让数据返回我们封装的格式
utils下新建common_list.py,重写list方法
from rest_framework.mixins import ListModelMixin from .common_response import APIResponse class CommonListModelMixin(ListModelMixin): def list(self, request, *args, **kwargs): res = super().list(request,*args,**kwargs) return APIResponse(result=res.data)
home/views.py继承咱们自己封装的CommonListModelMixin进行编写
from rest_framework.viewsets import GenericViewSet from .models import Banner from .serializer import BannerSerializer from utils.common_list import CommonListModelMixin class BannerView(GenericViewSet,CommonListModelMixin): queryset = Banner.objects.filter(is_delete=False,is_show=True).order_by('orders') serializer_class = BannerSerializer
返回格式对比