获取TypeError:“函数”对象不可订阅此错误在API创建过程中发生

2024-10-02 12:30:26 发布

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

def operator_logincheck_web(request, email=None):

    data = json.loads
    email = data['email']
    email = email.lower()
    password = data['password']
    if Operator.objects.filter(email = email).count() > 0:
        if Operator.objects.filter(email = email, password = password).count() > 0:
            loginobj = Operator.objects.get(email = email, password = password)
            send_data = {'status':"1", 'msg':"Login Successfull", 'user_id':str(loginobj.id)}
        else:
            send_data = {'status':"0", 'msg':"Incorrect Password"}
    else:
        send_data = {'status':"0", 'msg':"Incorrect Email"}
            
    return JsonResponse(send_data)

但是在操作员登录检查web中的终端文件“/home/sumit/Cricket/CricketApp/views.py”第1443行中获取此错误 电子邮件=数据['email'] TypeError:“函数”对象不可下标


Tags: sendwebiddataifobjectsemailstatus
1条回答
网友
1楼 · 发布于 2024-10-02 12:30:26

嗯,json.loads是一个函数,您已经将它赋回变量调用data,并且您正在从中查找email属性。您可以通过以下方式从request对象中查找有效负载:

data: dict = request.data

从错误中吸取教训

Python程序中的每个数据都由对象或对象之间的关系表示

这就是Pythondocumentation对对象的描述

Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann’s model of a “stored program computer”, code is also represented by objects.)

Python的函数是一流的对象。这意味着,(参考:Dan Bader

You can assign them to variables, store them in data structures, pass them as arguments to other functions, and even return them as values from other functions.

Python支持高阶函数。这意味着,(参考:Wikipedia

A higher-order function is a function that does at least one of the following, takes one or more functions as arguments (i.e. procedural parameters) or returns a function as its result.

>>> import json
>>> string_json = '{"key": "value"}'
>>> json.loads(string_json)
{'key': 'value'}
>>> 
>>> data = json.loads
>>> type(data)
function
>>> 
>>> data(string_json)
{'key': 'value'}

如果你不在这里发表评论,我希望你能理解这里发生了什么。您可以从Primer on Python Decorators文章中了解到许多与上述概念相关的内容

参考资料:-

相关问题 更多 >

    热门问题