day 97 区间过滤、 课程主页前端、 课程详情页前端、 课程单查接口、 文章分类接口、 七牛云视频托管、 后台搜索接口、 前台搜索页面完成
昨日回顾
# 1 celery定时更新首页轮播图接口(只要涉及到加入缓存的接口,一定涉及到更新---(双写一致性)),执行异步任务,定时任务(apscheduler)和延时任务
# 2 课程群查接口
-继承哪个类:
-如果想改路由(自动生成路由:ViewSetMixin),
-如果想跟数据库和序列化类打交道:GenericAPIView
-增删查改(5个视图扩展类)
-序列化类怎么写
-子序列化:teacher = TeacherSerializer()
-想序列化某个没有的字段,可以写在model类种或者序列化类中
-可能会重写validata或者create或者update方法
# 3 排序(继承:GenericAPIView,ListModelMixin)
-在视图类种配置
filter_backends=[OrderingFilter]
ordering_fields=['id', 'price', 'students']
-使用:http://127.0.0.1:8000/course/free/?ordering=-id
# 4 过滤 (继承:GenericAPIView,ListModelMixin)
-在视图类种配置
filter_backends=[SearchFilter]
search_fields=['id','name']
-使用:http://127.0.0.1:8000/course/free/?search=39
# 5 自定义过滤
-写一个类,继承BaseFilterBackend,重写filter_queryset方法,返回queryset对象,返回的对象就是过滤完的
-使用跟过滤配置一样
# 6 django-filter
-下载,配置在视图类中filter_backends=[DjangoFilterBackend]
-配置字段:filter_fields=['course_category'],支持自由字段和连表字段
今日内容
1 区间过滤
# 借助django-filter实现区间过滤
# 实现区间过滤
##########1 filters.py
class CourseFilterSet(FilterSet):
# 课程的价格范围要大于min_price,小于max_price
min_price = filters.NumberFilter(field_name='price', lookup_expr='gt')
max_price = filters.NumberFilter(field_name='price', lookup_expr='lt')
class Meta:
model=models.Course
fields=['course_category']
#####2 视图类中配置
-filter_backends=[DjangoFilterBackend]
# 配置类:(自己写的类)
-filter_class = CourseFilterSet
2 课程主页前端
- 课程分类:
- 全部
- {{category.name}}
- 筛 选:
- 默认
- 人气
- 价格
共{{course_total}}个课程
{{course.name}}
{{course.students}}人已加入学习
{{course.teacher.name}} {{course.teacher.title}} {{course.teacher.signature}}
共{{course.sections}}课时/已更新{{course.pub_sections}}课时
共{{course.sections}}课时/更新完成
- 0{{key+1}} | {{section.name}}
免费
{{course.discount_type}}
¥{{course.real_price}}元
原价:{{course.price}}元
¥{{course.price}}元
立即购买
3 课程详情页前端
# 1 新建FreeCourseDetail.vue
# 2 配置路由
{
path: '/free/detail/:pk',
name: 'FreeCourseDetail',
component: FreeCourseDetail
},
#3 安装 vue-video播放器
-cnpm install vue-video-player
-在main.js中配置
// vue-video播放器
require('video.js/dist/video-js.css');
require('vue-video-player/src/custom-theme.css');
import VideoPlayer from 'vue-video-player'
Vue.use(VideoPlayer);
#4 组件
{{course_info.name}}
{{course_info.students}}人在学 课程总时长:{{course_info.sections}}课时/{{course_info.pub_sections}}小时 难度:{{course_info.level_name}}
价格 ¥{{course_info.price}}
- 详情介绍
- 课程章节 (试学)
- 用户评论
- 常见问题
课程章节
共{{course_chapters.length}}章 {{course_info.sections}}个课时
第{{chapter.chapter}}章·{{chapter.name}}
-
{{chapter.chapter}}-{{section.orders}}
{{section.name}}免费
{{section.duration}} 
用户评论
常见问题
授课老师
{{course_info.teacher.name}}
{{course_info.teacher.title}}
{{course_info.teacher.signature}}
{{course_info.teacher.brief}}
4 课程单查接口
class CouresView(GenericViewSet,ListModelMixin,RetrieveModelMixin):
queryset = models.Course.objects.filter(is_delete=False,is_show=True).order_by('orders')
serializer_class = serializer.CourseModelSerializer
pagination_class = PageNumberPagination
# 过滤和排序
# filter_backends=[DjangoFilterBackend,OrderingFilter,SearchFilter]
# filter_backends=[DjangoFilterBackend,OrderingFilter,MyFilter]
filter_backends=[DjangoFilterBackend,OrderingFilter]
# # filter_backends=OrderingFilter
ordering_fields=['id', 'price', 'students']
# # search_fields=['course_category']
filter_fields=['course_category']
5 章节分类接口
#1 urls.py
router.register('chapters', views.CourseChapterView, 'coursechapter')
# 2 views.py
class CourseChapterView(GenericViewSet,ListModelMixin):
queryset = models.CourseChapter.objects.filter(is_delete=False,is_show=True)
serializer_class = serializer.CourseChapterSerializer
# 可以按照课程id来查
filter_backends = [DjangoFilterBackend]
filter_fields = ['course']
# 3 serializer.py
class CourseSectionSerializer(serializers.ModelSerializer):
class Meta:
model=models.CourseSection
fields=['name','orders','duration','free_trail','section_link','section_type_name']
class CourseChapterSerializer(serializers.ModelSerializer):
# 子序列化的方式
coursesections=CourseSectionSerializer(many=True)
class Meta:
model=models.CourseChapter
fields=['name','summary','chapter','coursesections']
6 七牛云视频托管
# 1 注册七牛云,实名认证
https://www.qiniu.com/
# 2 登录---》管理控制台---》对象存储
# 3 新建空间----》上传视频--》拿到视频连接
7 后台搜索接口
# urls.py
router.register('search', views.CouresSearchView, 'search')
# views.py
class CouresSearchView(GenericViewSet,ListModelMixin):
queryset = models.Course.objects.filter(is_delete=False,is_show=True)
serializer_class = serializer.CourseModelSerializer
pagination_class = PageNumberPagination
filter_backends=[SearchFilter]
search_fields=['name']
8 前台搜索页面完成
# 0 在Head.vue中相应的配置
# 1 vue带给另一个页面值
-路由:path: '/free/detail/:pk',this.$route.params.pk 从路径中取值
-search?word=dsa 去this.$route.query.word 从?后面的取
# 2 样式
总共搜索到 {{course_total}}课程
{{course.name}}
{{course.students}}人已加入学习
{{course.teacher.name}} {{course.teacher.title}} {{course.teacher.signature}}
共{{course.sections}}课时/已更新{{course.pub_sections}}课时
共{{course.sections}}课时/更新完成
- 0{{key+1}} | {{section.name}}
免费
{{course.discount_type}}
¥{{course.real_price}}元
原价:{{course.price}}元
¥{{course.price}}元
立即购买
没有搜索结果
9 支付宝
# 沙箱环境:https://openhome.alipay.com/platform/appDaily.htm
# https://opendocs.alipay.com/open/291/106097/ 下载软件,生成秘钥(公钥私钥)
# 取出公钥,配置再支付宝平台,它会生成一个支付宝公钥
#代码
# pip install python-alipay-sdk
from alipay import AliPay
# app_private_key_string = open("/path/to/your/private/key.pem").read()
# alipay_public_key_string = open("/path/to/alipay/public/key.pem").read()
app_private_key_string = """-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEApkFnp38PSslhn4wvRarL40+WtVpan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Gx9OQnsmpG2vxm4Oo0WnMw3mFIIvonFDZrxGd8rQU+DTWJZ21Hz/lnUugEjmdoJacvxeEEjEAgp02CoQFu21Ls8li4gKgTk+mYVyojHjhqNLp9GELadWMCgYEAi0RSXgLCEnT5p03zdgcsPOC3ByfT6jO+0GItWCX2HNN2mRhAeIQ0CcEKW4yEy56ptZQu1jtiFlpMTH4MNse/czCd15hCC/2G9zPhIgdRvjQsd/nznLA4HTIbJH5gC8EotHeHrSRATHh1kMTtbLn2KbWTA54XYK3tad0IQoWUz9ECgYBBc+kCJsb0shaRIRP5nmeb5tZCH7Yq9DgriwcIlK74cxW9fvL94n91Y6M8vSZ6MC7vF5wmcXMj3K1of04qizKbbKxCCYeOIHhTh0dB+ualDxu5WBG/6mLWR3HVHazqRVri9qoZ/3zNDx5CRNi81VpzOPmVtuos5d9sZZaNR3lkRQ==
-----END RSA PRIVATE KEY-----
"""
alipay_public_key_string = """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwU9mMZHHlQPE9FcxVtXOXhbWCtuDZLJRVCiofdbTVmRXrx47yGniPehwcKIsqhzhEaXBG2QhpIZUL8YsCav0mkrppoRvWOytuGyxNRESo8I6DWRs0aCq6P3AuiD9kSXET4dpAuRYT/+JrMXIZTycEts6vYYNAT9QivXJoa2FmiCQBAL3HP7F36pby9VstObilxXQcoBBJwEYGf2TK6moFFZ1dkloRr5Cfk/G82DpuVfrt1gr4OuIDWtcE3MZTrvDgTqtkRuwGF76FY3+8xUCUbJs1dL5cXYN7/b3jPcXVcdKXFj4WrOQd42ofE1BJWMxBW7L3Qlxue1vy+NGx/CuKwIDAQAB
-----END PUBLIC KEY-----
"""
alipay = AliPay(
appid="2016092000554611",
app_notify_url='http://127.0.0.1:8000/home/', # the default notify path
app_private_key_string=app_private_key_string,
# alipay public key, do not use your own public key!
alipay_public_key_string=alipay_public_key_string,
sign_type="RSA2", # RSA or RSA2
debug=True # False by default
)
alipay_url='https://openapi.alipaydev.com/gateway.do?'
order_string = alipay.api_alipay_trade_page_pay (
out_trade_no="20161112www4334",
total_amount=9999,
subject='你好世界',
return_url="https://www.luffycity.com/free-course",
notify_url="https://www.luffycity.com/free-course"
)
print(alipay_url+order_string)