示例#1
0
 def post(self, request, *args, **kwargs):
     userask_form = AddAskForm(request.POST)
     if userask_form.is_valid():
         userask_form.save(commit=True)
         return JsonResponse({"status": "success"})
     else:
         return JsonResponse({"status": "fail", "msg": "添加出错"})
示例#2
0
    def post(self, request, *args, **kwargs):
        userask_form = AddAskForm(request.POST)
        if userask_form.is_valid():
            # userask_form.save(commit=True)
            ask_numb = request.user.ask_numb
            if ask_numb > 1:

                userask = UserAsk(name=request.user.username)
                userask.mobile = userask_form.cleaned_data.get("mobile")
                userask.course_name = userask_form.cleaned_data.get(
                    "course_name")
                userask.save()

                # user = UserProfile(username=request.user.username)
                # i = user.ask_numb
                # user.ask_numb = i - 1
                # print(i)
                # user.save()
                i = request.user.ask_numb
                a = i - 1
                request.user.ask_numb = a
                request.user.save()
                return JsonResponse({"status": "success"})
            else:
                return JsonResponse({
                    "status": "fail",
                    "msg": "您的账号出现异常,请联系管理员"
                })
        else:
            return JsonResponse({"status": "fail", "msg": "添加出错"})
示例#3
0
 def post(self, request, *args, **kwargs):
     user_ask_from = AddAskForm(request.POST)
     if user_ask_from.is_valid():
         user_ask_from.save(commit=True)
         return JsonResponse({
             'status': 'success',
         })
     else:
         return JsonResponse({'status': 'fail', 'msg': '添加错误'})
示例#4
0
 def post(self, request, *args, **kwargs):
     userask_form = AddAskForm(
         request.POST)  # 表单对象均要接受request.POST参数,获取上传的表单的值
     if userask_form.is_valid():
         # modelform同时具有form和model的特性,使用model的save()方法,即可将数据存放到对应的数据库
         # 但是一定要设置commit=True参数(默认是该参数),表示将表单数据保存到数据库
         # 若commit设置为False,则是生成一个数据库model对象,可以对该对象进行增删改查之后,再使用save()进行保存
         userask_form.save(
             commit=True)  # 无论commit参数如何,该方法均返回一个数据库对象,区别是保存到数据库没有
         return JsonResponse({"status": "success"})
     else:
         return JsonResponse({"status": "fail", "msg": "添加出错"})
示例#5
0
文件: views.py 项目: L-960/MxOnline
 def post(self, request, *args, **kwargs):
     user_ask_form = AddAskForm(request.POST)
     # 如果表单合法
     if user_ask_form.is_valid():
         user_ask_form.save(commit=True)
         return JsonResponse({
             'status': 'success',
             'msg': '提交成功',
         })
     else:
         return JsonResponse({
             'status': 'error',
             'msg': '提交错误',
         })
示例#6
0
 def post(self, request, *args, **kwargs):
     userask_form = AddAskForm(request.POST)
     if userask_form.is_valid():
         """commit是提交"""
         userask_form.save(commit=True)
         return JsonResponse({
             'status': 'success',
             'msg': '提交成功',
         })
     else:
         return JsonResponse({
             'status': 'fail',
             'msg': '提交出错',
         })
示例#7
0
文件: views.py 项目: jzy33/MxOnline
 def post(self, request, *args, **kwargs):
     # modelform功能一,可以当form来使用,并执行表单验证is_valid
     userask_form = AddAskForm(request.POST)
     if userask_form.is_valid():
         # modelform功能二,可以当model来使用,实例化对象并保存到数据库,
         # 同时还可以返回这个对象
         user_ask = userask_form.save(commit=True)
         return JsonResponse({
             'status': 'success',
         })
     else:
         return JsonResponse({
             'status': 'fail',
             'msg': '添加出错',
         })
示例#8
0
 def post(self, request, *args, **kwargs):
     ask_form = AddAskForm(request.POST)
     if ask_form.is_valid():
         ask_form.save(commit=True)
         return JsonResponse({"status": "success"})
     else:
         errors = ask_form.errors  # 获取错误信息
         error_str = json.dumps(errors)  # 转换为str
         error_dict = json.loads(error_str)  # 转化为dict
         msg = ""  # 设定一个自定义msg字符串,用来返回错误
         for value in error_dict.values():  # 遍历error_dict的values,毕竟错误不止一个
             msg = "".join((str(msg), "\n", str(value)))  # 组装
         return JsonResponse({
             "status":
             "fail",
             "msg":
             "添加出错" + "\n" + msg.strip().replace('[', '').replace(
                 ']', '').replace('\'', "")
         })
示例#9
0
 def post(self, request, *args, **kwargs):
     """
     处理用户的咨询模块
     :param request:
     :param args:
     :param kwargs:
     :return:
     """
     userask_form = AddAskForm(request.POST)
     # 如果合法则保存
     if userask_form.is_valid():
         userask_form.save(commit=True)
         return JsonResponse({
             "status": "success",
              "msg": "提交成功"
         })
     else:
         return JsonResponse({
             "status": "fail",
             "msg": "提交出错"
         })