下面这个是接收前端ajax的函数,可以看到,当执行 else 下面的语句时添加数据库信息,但当我在前端页面上都填写好了之后,报错显示:
(1048, "Column 'gender' cannot be null")
-----------------------------------------------------------
在第6行代码打印那四个我在前端页面填写的数据,可以看到这四个变量都有值,gender打印出来的是 0,gender在model里我设置的是:
gender = models.BooleanField()
-----------------------------------------------------------
请问为什么添加不上,,,这都打印出来值了,,,
1 def studentAjax(req): 2 name = req.POST.get("name") 3 gender = req.POST.get("gender") 4 age = req.POST.get("age") 5 toClass = req.POST.get("toClass") 6 print(name, gender, age, toClass) 7 8 try: 9 if name=='' or gender=='' or age=='' or toClass=='': 10 return HttpResponse('信息中存在空值') 11 12 else: 13 models.StudentsNGA.objects.create(name=name) 14 models.StudentsNGA.objects.create(gender=gender) 15 models.StudentsNGA.objects.create(age=age) 16 models.StudentsNGA.objects.create(studentsToClass_id=toClass) 17 return HttpResponse('success') 18 19 except Exception as e: 20 print('错误信息:', e) 21 return HttpResponse('输入错误')
卧槽,问题出的有点sb,应该把所有参数都写进一个create里,,,