我到处搜索,但之前没有人指出此错误。obj将返回一个unicode对象,但它将返回以下错误Exception Type: AttributeError Exception Value:'unicode' object has no attribute 'pk'如果我对响应的结果进行硬编码,则它会起作用。CustomerAccount.pyfrom django.contrib.auth.models import User 检查登录 return userapi.pyresult = CustomerAccount.login(username, password) return HttpResponse(json.dumps(result), content_type="application/json")views.pyimport urllib2 import jsonres = 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”}}]
2 回答
饮歌长啸
TA贡献1951条经验 获得超3个赞
您输入的类型有误:
obj
是一个list
obj[0]
是一个dict
a
dict
没有pk
属性,但是您可以使用以下方法检索key的值"pk"
:obj[0]['pk']
添加回答
举报
0/150
提交
取消