码迷,mamicode.com
首页 > 其他好文 > 详细

10-后台主页模块设计

时间:2021-02-23 14:19:01      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:djang   util   图片   boolean   路由   dev   class   code   设计   

后台主页模块设计

一、创建home模块(app)

# 前提:在 luffy 虚拟环境下

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

# 2.创建app
python ../../manage.py startapp home

二、路由分发

1. 主路由:luffyapi/urls.py

from django.urls import path, re_path, include
urlpatterns = [
	# ...
    path(‘home/‘, include(‘home.urls‘)),
    # ...
]

2. 子路由:home/urls.py

from django.urls import path, re_path
urlpatterns = [

]

三、Banner数据表model设计

1. 基表:utils/model.py

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_npython
    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

2. banner表:home/models.py

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

    class Meta:
        db_table = ‘luffy_banner‘
        verbose_name_plural = ‘轮播图表‘

    def __str__(self):
        return self.title

3. 数据迁移:在大luffyapi路径下的终端

python manage.py makemigrations
python manage.py migrate

四、注册home模块(app):dev.py

INSTALLED_APPS = [
    ‘rest_framework‘,
    ‘home‘,
]

五、设计Banner数据接口

1. home/serializers.py

from rest_framework import serializers
from . import models
class BannerSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Banner
        fields = [‘title‘, ‘image‘, ‘link‘]

2. home/views.py

from rest_framework.viewsets import ModelViewSet
from rest_framework import mixins
from . import models, serializers

class BannerViewSet(ModelViewSet, mixins.ListModelMixin):
    queryset = models.Banner.objects.filter(is_delete=False, is_show=True).all()
    serializer_class = serializers.BannerSerializer

3. home/urls.py

from django.urls import path, include
from utils.router import router
from . import views
# 注册ViewSet的路由
router.register(‘banner‘, views.BannerViewSet, ‘banner‘)

urlpatterns = [
    path(‘‘, include(router.urls)),
]

4. 接口

http://127.0.0.1:8000/home/banner
http://localhost:8000/home/banner

10-后台主页模块设计

标签:djang   util   图片   boolean   路由   dev   class   code   设计   

原文地址:https://www.cnblogs.com/borntodie/p/14430940.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!