后台主页模块设计

home模块

创建home模块

1
2
3
4
5
6
7
前提:在 luffy 虚拟环境下

1.终端从项目根目录进入apps目录
>: cd luffyapi & cd apps

2.创建app
>: python ../../manage.py startapp home

路由分发

主路由:luffyapi/urls.py
1
2
3
4
5
6
from django.urls import path, re_path, include
urlpatterns = [
# ...
path('home/', include('home.urls')),
# ...
]
子路由:home/urls.py
1
2
3
4
from django.urls import path, re_path
urlpatterns = [

]

Banner数据表model设计

utils/model.py
1
2
3
4
5
6
7
8
9
10
11
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='优先级')
  # 参考字段
  phone = models.ForeignKey("Teacher", related_name='teacher_bind', on_delete=models.CASCADE,
verbose_name="手机号",null=True,db_constraint=False)


classMeta:
abstract = True
home/models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from django.db import models
from utils.model import BaseModel

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='详情') # 也可以用详情表,宽高出处

classMeta:
db_table = 'luffy_banner'
verbose_name_plural = '轮播图表'

def__str__(self):
return self.title
数据迁移:在大luffyapi路径下的终端
1
2
>: python manage.py makemigrations
>: python manage.py migrate

注册home模块:dev.py

1
2
3
4
5
INSTALLED_APPS = [
# ...
'rest_framework',
'home',
]

设计Banner数据接口

home/serializers.py
1
2
3
4
5
6
from rest_framework import serializers
from . import models
class BannerSerializer(serializers.ModelSerializer):
classMeta:
model = models.Banner
fields = ['title', 'image', 'link']
home/views.py
1
2
3
4
5
6
7
from django.shortcuts import render
from django.core.cache import cache
from rest_framework.viewsets import GenericViewSet
from rest_framework import mixins
from . import models, serializers
from luffyapi.utils.response import APIResponse
class BannerViewSet(GenericViewSet, mixins.ListModelMixin):
queryset = models.Banner.objects.filter(is_delete=False, is_show=True).all()
serializer_class = serializers.BannerSerializer
def list(self, request, *args, **kwargs):
banner_list = cache.get('banner_list')
if not banner_list:
response = super().list(request, *args, **kwargs)
cache.set('banner_list',response.data)
return APIResponse(msg=response.data)
else:
print(banner_list)
return APIResponse(msg=banner_list)
home/urls.py
1
2
3
4
5
6
7
8
9
from django.urls import path, include
from . import views
from rest_framework import routers
router = routers.SimpleRouter()
# 注册ViewSet的路由
router.register('banners', views.BannerViewSet, 'banner')
urlpatterns = [
path('', include(router.urls)),
 
接口
1
http://localhost:8000/home/banners/

posted on 2020-12-09 15:56  Plyc  阅读(88)  评论(0编辑  收藏  举报

导航