Django API TypeError:字符串索引必须是整数,而不是str

2024-10-05 13:18:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个API来将数据保存到模型中,目前正在处理string indices must be integers, not str的错误。我已经在网上寻找解决方案,包括这个网站上关于这个错误的许多问题,到目前为止还没有找到解决方案。我现在在代码中包含了一个try-except语句,因为服务器只是说错误是内部的,try-except提供了对代码出错原因的更多了解。我已经仔细检查过了,相当肯定我的铸件是正确的。我在下面包含了我的代码。如有任何建议,我们将不胜感激

views.py

def send_data(request):

    API_ENDPOINT = "http://127.0.0.1:8000/data"
    my_dict = {
        'eventtransactions_id': 0000111,
        'profitcenter_id': 7,
        'customer_gender': 'F',
        'customer_firstname': 'Jan',
        'customer_lastname': 'Smith',
        'actualdatetime': "02-15-2020 18:54:58",
        'custnum': 8880005643,
        'birthdate': "02-15-2000 18:54:58",
        'membertype': 'Student',
        'eventname': 'Location',
        },{
        'eventtransactions_id': 1234567,
        'profitcenter_id': 7,
        'customer_gender': 'M',
        'customer_firstname': 'Joe',
        'customer_lastname': 'Smith',
        'actualdatetime': "02-15-2020 18:54:58",
        'custnum': 8880005643,
        'birthdate': "07-15-1999 18:54:58",
        'membertype': 'Student',
        'eventname': 'Location',
        }
    json_dict = json.dumps(my_dict)
    requests.post(API_ENDPOINT, data=json_dict)

    return HttpResponse("I'm broken.")


@csrf_exempt
def receive_data(request):
    if request.method == 'POST':
        my_data = request.body
        data = json.loads(my_data)
        json_data = json.dumps(data)

        try:
            new_swipe = FitnessCenterSwipe.objects.create(
                profitcenter_id= int(json_data['profitcenter_id']),
                eventtransactions_id= int(json_data['eventtransactions_id']),
                custnum= int(json_data['custnum']),
                customer_lastname= json_data['customer_lastname'],
                customer_firstname= json_data['customer_firstname'],
                actualdatetime= datetime.datetime.strptime(json_data['actualdatetime'], '%m-%d-%Y %H:%M:%S'),
                eventname= json_data['eventname'],
                membertype= json_data['membertype'],
                birthdate= datetime.datetime.strptime(json_data['birthdate'], '%m-%d-%Y'),
                customer_gender= json_data['customer_gender'],
            )
            print("\n")
            print("ENTRY CREATED")
        except Exception as e:
            print(e)

    else:
        print("NOT POST REQUEST")
        return HttpResponse("I'm broken 1.")

models.py

class FitnessCenterSwipe(models.Model):
    profitcenter_id = models.IntegerField()
    eventtransactions_id = models.IntegerField() #unique?
    custnum = models.IntegerField()
    customer_lastname = models.CharField(max_length=150)
    customer_firstname = models.CharField(max_length=150)
    actualdatetime = models.DateTimeField()
    eventname = models.CharField(max_length=150)
    membertype = models.CharField(max_length=150)
    birthdate = models.DateTimeField()
    customer_gender = models.CharField(max_length=6)


Tags: idjsondatamodelscustomerfirstnamegenderbirthdate
1条回答
网友
1楼 · 发布于 2024-10-05 13:18:01
@csrf_exempt
def receive_data(request):
if request.method == 'POST':
    my_data = request.body
    data = json.loads(my_data)
    json_data = json.dumps(data) ## This line is the problem.

您需要在try语句之后进行转储。当您转储该值时,它将变为字符串,并在加载时dict

希望有帮助

相关问题 更多 >

    热门问题