Problem with Facebook + django

I am trying to write a facebook application where a user can see the history of his friends. Everything seems to be working fine until I try to save status information in my DB. here is the code:

   class UserStatus(models.Model):
        facebookid = models.IntegerField()
        time = models.IntegerField()
        status_msg = models.CharField(max_length = 2000)



@facebook.require_login()
 def canvas(request):
    # Get the User object 
    user, created = FacebookUser.objects.get_or_create(id = request.facebook.uid)
    user_lastname = request.facebook.users.getInfo([request.facebook.uid], ['last_name'])[0]['last_name']
    query = "SELECT time,message FROM status WHERE uid=%s" % request.facebook.uid
    result = request.facebook.fql.query(query)

So, the result will give me all the status information. so my problem is that it gives an error when I try to save it.

 userstatus = UserStatus()
  for item in result:
  userstatus.facebookid = request.facebook.uid
  userstatus.time = item.time


userstatus.msg = item.message
  userstatus.save()

error: Errors while loading the page from the application

Received HTTP 500 Error Code on Boot

So how can I fix this.

thanks.

+3
source share
1 answer

First you have to check if you get results,

result = request.facebook.fql.query(query)

, , (uid - , - .

, python, / JSON.

, python JSON, , JSON, , python,

import simplejson
result = simpljson.loads(result) # if result was a JSON string
result = simpljson.loads(simplejson.dumps(result)) # if result was a JSON object

, { "time": 123456, "messaage": "xyz" }.

for item in result:
    userstatus = UserStatus() 
    userstatus.facebookid = request.facebook.uid
    userstatus.time = item["time"]
    userstatus.msg = item["message"]
    userstatus.save()

.

0

Source: https://habr.com/ru/post/1708401/


All Articles