0
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)

But geting this error on terminal File "/home/sumit/Cricket/CricketApp/views.py", line 1443, in operator_logincheck_web email = data['email'] TypeError: 'function' object is not subscriptable

1
  • How exactly are you expecting data = json.loads to work? Commented Jul 21, 2020 at 6:09

1 Answer 1

1

Well, json.loads is a function, You've assigned it back to the variable call data and you're looking email property from it. You can find payload from the request object in this way,

data: dict = request.data

LEARN FROM MISTAKES

Every data in a Python program is represented by objects or relations between objects.

This is what Python documentation describe about Objects.

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’s functions are first-class objects. Which mean, (Ref: 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.

and Python support Higher-order functions. Which mean, (Ref: 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'}

I hope you can understand what's going on here, if not comment here. You can learn lot of things related to the above concepts from Primer on Python Decorators article.

References:-

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.