【问题标题】:Json keyerror pythonJson keyerror python
【发布时间】:2020-12-27 12:29:17
【问题描述】:

这是我在尝试“POST”时遇到的错误

views.py 文件

def scent_data_pass(request):
if request.method == 'GET':
    response = {}
    response["data"] = "Data has been saved"

    return JsonResponse(response, status=200)

if request.method == 'POST':
    response = {}
    json_data = json.loads(request.body.decode('utf-8'))
    name = json_data["name"]
    uuid = json_data["datatuuid"]
    saved = Scent_raw(
        name=name,
        device_uuid = uuid

    )
    saved.save()
    try:
        response['result'] = 'Success'
        response['message'] = saved.datatuuid
    except:
        response['result'] = 'Ouch!'
        response['message'] = 'Script has not ran correctly'
    return JsonResponse(response)

models.py 文件

class Scent_raw(TimeStampedModel):
name = models.CharField(max_length=100, default="", editable=False)
datatuuid = models.UUIDField(max_length=100, default="", editable=False)
class Meta:
    verbose_name = 'Scent raw data'
    verbose_name_plural = "Scent raw data"

def __str__(self):
    return f'{self.id}'

为什么我在这里遇到 keyerror ?

【问题讨论】:

  • 请编辑您问题中的 Python 代码,使其缩进正确,并在您的问题中添加完整的错误消息,以确保发生错误的行很明显。

标签: python json django api django-rest-framework


【解决方案1】:

在邮递员中,您正在添加与 json 不同的查询参数。进入body标签,选择JSON格式,输入以下{"name":"mitesh", "datauuid":"xyz"}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 2018-12-15
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    相关资源
    最近更新 更多