def post(self, request): ask_form = UserAskForm(request.POST) if ask_form.is_valid(): ask_form.save() return JsonResponse({'status': 'success', 'msg': ''}) else: return JsonResponse({'status': 'fail', 'msg': '添加出错'})
def post(self,request): ask_form = UserAskForm(request.POST) if ask_form.is_valid(): ask_form.save(commit=True) #这里可以将form表单直接提交到数据库,如果设定commit=TRUE的话,那么不但会提交到数据库,还会保存再数据库里面 return HttpResponse("{'status':'success'}",content_type='application/json') else: return HttpResponse("{'status':'fail','msg':'提交发生错误'}",content_type='application/json')
def post(self, request): userask_form = UserAskForm(request.POST) if userask_form.is_valid(): user_ask = userask_form.save(commit = True) return HttpResponse('{"status":"success"}', content_type="application/json") else: return HttpResponse('{"status":"fail", "msg":"添加出错"}', content_type="application/json")
def post(self, request): userask_form = UserAskForm(request.POST) if userask_form.is_valid(): userask_form.save(commit=True) # 保存 return JsonResponse({'status': 'success'}) else: return JsonResponse({'status': 'fail', 'msg': '您的字段有错误,请检查'})
def post(self, request): userask_form = UserAskForm(request.POST) if userask_form.is_valid(): user_ask = userask_form.save(commit=True) return HttpResponse('{"status":"success"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"您的字段有错误,请检查"}', content_type='application/json')
def post(self, request): userask_form = UserAskForm(request.POST) if userask_form.is_valid(): user_ask = userask_form.save(commit=True) return HttpResponse("{'status':'success'}", content_type='application/json') else: return HttpResponse("{'status':'fail','msg:':{0}}".format(userask_form.errors), content_type='application/json')
def post(self, request): user_askform = UserAskForm(request.POST) if user_askform.is_valid(): user_askform.save(commit=True) return JsonResponse("{'status':'success'}") else: return JsonResponse("{'status':'fail','msg':{0}}".format( user_askform.errors))
def post(request): userask_form = UserAskForm(request.POST) if userask_form.is_valid(): userask_form.save() return HttpResponse('{"status": "success"}', content_type="application/json") else: return HttpResponse('{"status": "fail", "msg": "提交错误"}', content_type="application/json")
def post(self, request): userask_form = UserAskForm(request.POST) if userask_form.is_valid(): user_ask = userask_form.save(commit=True) return HttpResponse('{"status":"success" }', content_type='application/json') else: return HttpResponse("{'status':'fail', 'msg':'提交出错'}", content_type='application/json')
def post(self, request): ask_form = UserAskForm(request.POST) if ask_form.is_valid(): user_ask = ask_form.save(commit=True) # 将form中的内容保存到数据库中 return HttpResponse('{"status": "success"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg": "添加错误"}', content_type='application/json')
def post(self, request): userask_form = UserAskForm(request.POST) if userask_form.is_valid(): user_ask = userask_form.save(commit=True) # 如果保存成功,返回json字符串,后面content type是告诉浏览器返回的数据类型 return HttpResponse('{"status":"success"}', content_type='application/json') else: # 如果保存失败,返回json字符串,并将form的报错信息通过msg传递到前端 return HttpResponse('{"status":"fail", "msg":"添加出错"}', content_type='application/json')
def post(self, request): userask_form = UserAskForm(request.POST) if userask_form.is_valid(): user_ask = userask_form.save(commit=True) return HttpResponse("{\"status\":\"success\"}", content_type='application/json') else: return HttpResponse("{\"status\":\"fail\",\"msg\":\"添加出错\"}", content_type='application/json')
def post(self, request): resp = {} userask_form = UserAskForm(request.POST) if userask_form.is_valid(): userask_form.save(commit=True) resp['status'] = 'success' else: resp['status'] = 'fail' resp['msg'] = userask_form.errors return HttpResponse(json.dumps(resp), content_type="application/json")
def post(request): resp = {'status': 'success'} userask_form = UserAskForm(request.POST) if userask_form.is_valid(): user_ask = userask_form.save(commit=True) return HttpResponse(json.dumps(resp, ensure_ascii=False)) else: resp['status'] = 'fail' resp['msg'] = '添加出错' return HttpResponse(json.dumps(resp, ensure_ascii=False))
def post(self, request): userask_form = UserAskForm(request.POST) # 判断form是否有效 if userask_form.is_valid(): # 这个form有model属性 user_ask = userask_form.save(commit=True) return HttpResponse('{"status": "success"}', content_type='application/json') else: # 如果保存失败,返回json字符串,并将form的报错信息通过msg传递到前端 return HttpResponse('{"status": "fail", "msg": {0}}'.format(userask_form.errors), content_type='application/json')
def post(self, request): data_temp = UserAskForm(request.POST) if data_temp.is_valid(): data_temp.save() return HttpResponse(json.dumps({'status': 'success'}), content_type='application/json') else: return HttpResponse(json.dumps({ 'status': 'fail', 'msg': "咨询失败请重新输入" }), content_type='application/json')
def post(self, request): userask_form = UserAskForm(request.POST) if userask_form.is_valid(): user_ask = userask_form.save(commit=True) return HttpResponse(json.dumps({'status': 'success'}), content_type='application/json') else: return HttpResponse(json.dumps({ 'status': 'fail', 'msg': u'添加出错' }), content_type='application/json')
def post(self, request): userask_form = UserAskForm(request.POST) if userask_form.is_valid(): # 这里是modelform和form的区别 # 它有model的属性 # 当commit为true进行真正保存 user_ask = userask_form.save(commit=True) # 这样就不需要把一个一个字段取出来然后存到model的对象中之后save # 如果保存成功,返回json字符串,后面content type是告诉浏览器的 return HttpResponse('{"status":"success"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"信息有错"}', content_type='application/json')
def post(self, request): userask_form = UserAskForm(request.POST) if userask_form.is_valid(): # 这里是modelform和form的区别 # 它有model的属性 # 当commit为true进行真正保存 user_ask = userask_form.save(commit=True) # 如果保存成功,返回json字符串,后面content type是告诉浏览器的, return HttpResponse("{'status': 'success'}", content_type='application/json') else: # 如果保存失败,返回json字符串,并将form的报错信息通过msg传递到前端 return HttpResponse("{'status':'fail', 'msg':{0}}".format( userask_form.errors), content_type='application/json')
def post(self, request): useraskform = UserAskForm(request.POST) if useraskform.is_valid(): name = request.POST.get('name') mobile = request.POST.get('mobile') course_name = request.POST.get('course_name') userAsk = UserAsk() userAsk.name = name userAsk.mobile = mobile userAsk.course_name = course_name userAsk.save() # user_ask = useraskform.save(commit=True) # 如果保存成功,返回json字符串,后面content type是告诉浏览器返回的数据类型 return HttpResponse('{"status":"success"}', content_type='application/json') else: # 如果保存失败,返回json字符串,并将form的报错信息通过msg传递到前端 return HttpResponse('{"status":"fail", "msg":"添加出错"}', content_type='application/json') pass
def post(self, request): userask_form = UserAskForm(request.POST) # 判断该form是否有效 if userask_form.is_valid(): # 这里是modelform和form的区别,它有model的属性save; # 当commit为true时,提交保存至数据库中:将Form中的field传输至所调用的model中,并保存到数据库; # 这样就不需要把一个一个字段取出来然后存到model的对象中之后save; user_ask = userask_form.save(commit=True) # 数据的提交是ajax的异步形式,不做整个页面的刷新,所以在返回的时候不应该是页面,而应该是json的数据; # 因为在前端操作的时候是ajax方式提交过来的,所以返回页面也是无效的; # 如果保存成功,返回json字符串,后面content type是告诉浏览器的; # 注意此处的数据发送格式:前面是提交的状态,后面是指明json字符串的所属格式(因为浏览器接收到的是一串json字符串); # content_type='application/json'这个是固定的浏览器支持的格式;浏览器会对这串数据进行解析; return HttpResponse("{'status': 'success'}", content_type='application/json') else: # 如果保存失败,返回json字符串,并将form的报错信息通过msg传递到前端 # return HttpResponse("{'status': 'fail', 'msg':{0}}".format(userask_form.errors), content_type='application/json') # 因为userask_form.errors中的提示信息较多,所以在此处统一设置错误提示为'添加出错'; return HttpResponse('{"status":"fail", "msg":"添加出错"}', content_type='application/json')