本帖最后由 thepoy 于 2019-9-16 14:41 编辑
PyCharm中的REST工具。
models如下:
[Python] 纯文本查看 复制代码 class Book(models.Model):
b_name = models.CharField(max_length=32)
b_price = models.FloatField(default=10)
def to_dict(self):
return {'id': self.id, 'b_name': self.b_name, 'b_price': self.b_price}
views如下:
[Python] 纯文本查看 复制代码 @csrf_exempt
def books(request):
if request.method == 'GET':
data = {
'status': 200
}
return JsonResponse(data)
elif request.method == 'POST':
b_name = request.POST.get('b_name')
b_price = request.POST.get('b_price')
book = Book()
book.b_name = b_name
book.b_price = b_price
book.save()
data = {
'status': 201,
'msg': '添加成功',
'data': book.to_dict()
}
return JsonResponse(data)
用REST测试:
报错:
IntegrityError at /api/books/
(1048, "Column 'b_name' cannot be null")
Request Method: POST
Request URL: http://127.0.0.1:8000/api/books/?b_name=%E8%A5%BF%E6%B8%B8%E8%AE%B0&b_price=9.9
Django Version: 2.2.5
Exception Type: IntegrityError
Exception Value:
(1048, "Column 'b_name' cannot be null")
能力有限,实在看不出来是哪里的问题,请大佬指教一下
===================================
已解决:
pycharm的问题,换postman行了,垃圾pycharm!毁我青春!
|