I have search every where but no one stated this error before.
The obj will return a unicode object but it will return the following error
Exception Type: AttributeError
Exception Value:'unicode' object has no attribute 'pk'
It works if I hard code the result from the response.
CustomerAccount.py
from django.contrib.auth.models import User
check login
return user
api.py
result = CustomerAccount.login(username, password)
return HttpResponse(json.dumps(result), content_type="application/json")
views.py
import urllib2
import json
res = urllib2.urlopen("http://127.0.0.1:8000/api/login?username=admin&password=admin").read()
obj = json.loads(res)
print obj[0].pk
Result of print obj:
[{"pk": 1, "model": "auth.user", "fields": {"username": "admin", "first_name": "Admin", "last_name": "admin", "is_active": true, "is_superuser": true, "is_staff": true, "last_login": "2013-05-29T08:08:43.859Z", "groups": [], "user_permissions": [], "password": "pbkdf2_sha256$10000$1HdCOPgsoXvx$8jjOpTFVcVAtUshpjJDPEGs/TRq7jeJ2T/2i55FIPeM=", "email": "[email protected]", "date_joined": "2013-05-15T07:59:30Z"}}]
print obj[0]['pk']
? – Austin Phillips