zoukankan      html  css  js  c++  java
  • 发布品论接口

    1.1 course/urls.py 中添加路由

    urlpatterns = [
        path('comment/', views.CommentView.as_view()), # 查询用户名手机号使用量的视图,/user/count/
    ]

    1.2 course/views.py 添加评论视图函数

    from rest_framework_jwt.utils import jwt_decode_handler
    from rest_framework.response import Response
    from .models import Comment
    
    class CommentView(APIView):
    
        def post(self, request):
            """
            1.token
            2.course_id
            3.content
            4.fid
            """
    
            # 1.获取参数
            token = request.data.get('token')
            course_id = request.data.get('course_id')
            content = request.data.get('content')
    
            # 2.验证是否为空
            if not all([token, course_id,content]):
                return Response({'code':9999,'msg':'参数不全'})
    
            # 3.通过jwt token获取用户: {'user_id': 2, 'username': 'lisi', 'exp':1561504444, 'email': ''}
            toke_user = jwt_decode_handler(token)
            user_id = toke_user.get('user_id')
            fid = request.data.get('fid')
    
            # 4.创建评论
            # 4.1 如果没有评论父ID证明是第一次评论,直接创建到评论表
            if not fid:
                Comment.objects.create(
                    user_id=user_id,
                    course_id = course_id,
                    content = content
                )
            else:
                to_user = ''
            return Response({'code': 0, 'msg': '发表品论成功'})

    2.测试接口

    Http://192.168.56.100:8888/course/comment/

    3.JWT通过token获取用户信息

    # 通过用户token获取用户信息

    from rest_framework_jwt.utils import jwt_decode_handler

    toke_user = jwt_decode_handler(token)

    # {'user_id': 2, 'username': 'lisi', 'exp': 1561504444, 'email': ''}

  • 相关阅读:
    计算机中的进制和编码
    操作系统简史
    电脑结构和CPU、内存、硬盘三者之间的关系
    电脑简史
    使用开源my-deploy工具实现开发环境的代码自动化部署
    使用Let’s Encrypt创建nginx免费SSL证书
    VM ESXI 服务器虚拟化资料积累
    python mysql连接函数
    python日期格式转换小记
    Python模块学习
  • 原文地址:https://www.cnblogs.com/gaodenghan/p/13942497.html
Copyright © 2011-2022 走看看