我到处搜索,但之前没有人指出此错误。
obj将返回一个unicode对象,但它将返回以下错误
Exception Type: AttributeError
Exception Value:'unicode' object has no attribute 'pk'
如果我对响应的结果进行硬编码,则它会起作用。
CustomerAccount.py
from django.contrib.auth.models import User
检查登录
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
打印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”:“ admin@admin.com”,“ date_joined”:“ 2013-05-15T07:59:30Z”}}]
饮歌长啸
相关分类