Exemple #1
0
    def post(self, request, *args, **kwargs):

        # 如果未登录,封装数据告诉ajax,ajax会返回登录页面
        # status和msg,一定要和前端的ajax一致,否则ajax处理不了
        if not request.user.is_authenticated:
            return JsonResponse({
                "status": "fail",
                "msg": "用户未登录",
            })

        # 拿到数据给modelForm验证
        comment_form = CommentsForm(request.POST)
        # 字段检验通过
        if comment_form.is_valid():
            course = comment_form.cleaned_data['course']
            comments = comment_form.cleaned_data['comments']

            # 手动生成评论对象,保存其user,comment,course三种属性
            comment = CourseComments()
            comment.user = request.user
            comment.comments = comments
            comment.course = course
            comment.save()

            return JsonResponse({
                "status": "success",
            })

        # 字段检验不通过
        else:
            return JsonResponse({
                "status": "fail",
                "msg": "参数格式错误",
            })
Exemple #2
0
    def post(self, request, *args, **kwargs):
        '''
        用户评论
        '''
        # request.user.is_authenticated:用来判断用户是否登陆
        if not request.user.is_authenticated:
            return JsonResponse({
                "status": "fail",
                "msg": "用户未登录",
            })

        comment_form = CommentsForm(request.POST)
        if comment_form.is_valid():
            course = comment_form.cleaned_data['course']
            comments = comment_form.cleaned_data['comments']

            comment = CourseComments()
            comment.user = request.user
            comment.comments = comments
            comment.course = course
            comment.save()

            return JsonResponse({
                "status": "success",
            })
        else:
            return JsonResponse({
                "status": "fail",
                "msg": "参数错误",
            })
Exemple #3
0
    def post(self, request, *args, **kwargs):
        """
        用户收藏,取消收藏
        """
        if not request.user.is_authenticated:
            return JsonResponse({
                "status":"fail",
                "msg":"用户未登录"
            })

        comment_form = CommentsForm(request.POST)
        if comment_form.is_valid():
            course = comment_form.cleaned_data["course"]
            comments = comment_form.cleaned_data["comments"]

            comment = CourseComments()
            comment.user = request.user
            comment.comments = comments
            comment.course = course
            comment.save()

            return JsonResponse({
                "status": "success",
            })
        else:
            return JsonResponse({
                "status": "fail",
                "msg": "参数错误"
            })
Exemple #4
0
    def post(self, request, *args, **kwargs):
        '''
        用户收藏,取消收藏
        :param request:
        :param org_id:
        :param args:
        :param kwargs:
        :return:
        '''
        comment_form = CommentsForm(request.POST)
        # 如果用户未登录
        if not request.user.is_authenticated:
            return JsonResponse({
                'status': 'fail',
                'msg': '用户未登录'
            })
        if comment_form.is_valid():
            course = comment_form.cleaned_data['course']
            comments = comment_form.cleaned_data['comments']

            comment = CourseComments()
            comment.user = request.user
            comment.comments = comments
            comment.course = course
            comment.save()
            return JsonResponse({
                'status': 'success',
            })
        else:
            return JsonResponse({
                'status': 'fail',
                'msg': '参数错误'
            })