python - Problems Accessing dictionary in Django -
i able access dictionary fine in django python shell using p['cover']['source']
. can access 'source' in templates using dot notation, however, when attempting access p['cover']['source']
in views keyerror. able access 'cover' using p.get('cover','none')
need p['cover']['source']
, have no idea how access this. please :-)
views.py image_table = [] n in likes: link = n.facebook_id p = graph.get_object(str(link)) #image = p['cover']['source'] //this returns keyerror #image = p['cover'][0]['source'] //this returns keyerror = 0 image = p.get('cover','none')//this returns first dictionary image_table.append(image)
some users may not have cover picture, causing keyerror
when access dictionary. use try/except block prevent error:
try: image = p['cover']['source'] except keyerror: pass # or other alternative without cover picture
Comments
Post a Comment