python web(bottle框架)知行合一之-简单知识付费平台-”全栈“实践(11)---分页获取课程列表信息接口

python web(bottle框架)知行合一之-简单知识付费平台-”全栈“实践(11)---分页获取课程列表信息接口

PS:笔记只是为了更好表达我怎么语言表述,有些时候可能难免废话一推!
因知识有限, 如有错误, 欢迎指正!

每日细语:没有雨伞的孩子只能在努力的在雨中奔跑~

续言

本期主要讲解的是获取课程列表接口:

需求分析

前端的页面通过请求,进行分页加载全部课程列表信息,如果没有对应的课程,则显示暂无课程!

需求实现过程:

1:先批量插入测试数据:

# 批量插入一些课程信息
def InsertManyTest():
    with session_scope():
        data = [
            {
                'course_classify_code': '001',
                'course_code_id': '1000',
                'name': '爱因斯坦',
                'title': '爱因斯坦的伟大',
                'describe_simple': 0,
                'describe_detailed': '不想详细的描述太多!!',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 0,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 1,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '002',
                'course_code_id': '1001',
                'name': '爱因斯坦2',
                'title': '爱因斯坦的伟大2',
                'describe_simple':0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '003',
                'course_code_id': '1002',
                'name': '爱因斯坦3',
                'title': '爱因斯坦的伟大3',
                'describe_simple':0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '004',
                'course_code_id': '1005',
                'name': '爱因斯坦5',
                'title': '爱因斯坦的伟大5',
                'describe_simple': 0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '006',
                'course_code_id': '1006',
                'name': '爱因斯坦6',
                'title': '爱因斯坦的伟大6',
                'describe_simple': 0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '007',
                'course_code_id': '1007',
                'name': '爱因斯坦7',
                'title': '爱因斯坦的伟大7',
                'describe_simple': 0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '008',
                'course_code_id': '1008',
                'name': '爱因斯坦8',
                'title': '爱因斯坦的伟大8',
                'describe_simple': 0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '009',
                'course_code_id': '1009',
                'name': '爱因斯坦9',
                'title': '爱因斯坦的伟大9',
                'describe_simple': 0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '0010',
                'course_code_id': '10010',
                'name': '爱因斯坦10',
                'title': '爱因斯坦的伟大10',
                'describe_simple': 0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '0011',
                'course_code_id': '10011',
                'name': '爱因斯坦11',
                'title': '爱因斯坦的伟大11',
                'describe_simple':0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '0012',
                'course_code_id': '10012',
                'name': '爱因斯坦12',
                'title': '爱因斯坦的伟大12',
                'describe_simple': 0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '0013',
                'course_code_id': '10013',
                'name': '爱因斯坦13',
                'title': '爱因斯坦的伟大13',
                'describe_simple': 0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '0014',
                'course_code_id': '10014',
                'name': '爱因斯坦14',
                'title': '爱因斯坦的伟大14',
                'describe_simple': 0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '0015',
                'course_code_id': '10015',
                'name': '爱因斯坦15',
                'title': '爱因斯坦的伟大15',
                'describe_simple': 0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '0016',
                'course_code_id': '10016',
                'name': '爱因斯坦16',
                'title': '爱因斯坦的伟大16',
                'describe_simple': 0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            },
            {
                'course_classify_code': '0017',
                'course_code_id': '10017',
                'name': '爱因斯坦17',
                'title': '爱因斯坦的伟大17',
                'describe_simple': 0,
                'describe_detailed': '不想详细的描述太多!!3',
                'img_cover': 'http:\/\/ss.gxtcwl.com\/images\/fe3ebd121b239eeca2899f8d0925d963.jpg?imageView2\/1\/w\/200\/h\/200\/q\/100',
                'state': 2,
                'all_num_count': 20,
                'is_down': 1,
                'is_sell': 1,
                'is_flee': 1,
                'price': 50,
                'has_learn_num': 200,
                'has_updata_num_count': 20,
                'is_vip_flee': 0,
                'author': '爱因斯坦',
                'type': 3
            }

        ]
        query = Course.insert_many(data).execute()  # 插入了多个

查看数据库插入结果:

image.png

2:分页查询请求读取数据库记录信息:
定义接口:


image.png

3:在main.py导入该接口模块:

# 导入所有的服务接口模块
from business_logic.api_offer import login,register,get_course

4:编写对应的获取课程业务逻辑

image.png

5:在course_logic.py定义相关数据分页请求处理

  from business_logic.db_model.knowledgepay_model import session_scope, Course

# 分页查询
def get_course_paginate(page_num=1):
    with session_scope():
        result = Course.select().dicts().order_by(Course.id).paginate(int(page_num), 4)
        result_list = []
        is_ends = False
        if result:
            for row in result:
                result_list.append(row)
        if not result_list:
            is_ends = True
        return result_list, is_ends

6:在get_course.py路由的模块:

from bottle import get, post, request
from base_framework.net import web_helper, session_helper
from business_logic.api_db_logics import course_logic
from base_framework.utils import json_helper


@get('/api/v1/course/get/')
@post('/api/v1/course/get/')
def callback():
    pass
    '''获取课程列表'''
    page_num = web_helper.get_query('page_num', 'page_num', False)  # 默认开启 不能为空的验证
    if not page_num:
        page_num = 1

    '''1:连接数据库并读取数据列表'''
    result_list, is_ends = course_logic.get_course_paginate(page_num=page_num)
    return_date = {
        "data": result_list
    }
    if is_ends and int(page_num) > 1:
        return web_helper.return_msg("9992", "加载完毕", result_list)

    if not result_list:
        return web_helper.return_msg("9991", "暂无课程", result_list)

    return web_helper.return_msg("0000", "获取成功", return_date)

接口测试:

image.png
image.png

结束

获取课程的逻辑其实就是把所有课程给提取出来。因为我们还没涉及到按分类进行读取,所哟可以暂时就全部的提取!

以上笔记纯属个人学习实践总结,有兴趣的同学可以加群一起学习讨论QQ:308711822

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容