0

Inside of my Django view I am trying to retrieve results from my database and then pass them on to my template with the following code:

f = request.GET.get('f')

  try:
    fb_friends_found= UserProfile.objects.filter(facebookid__in=f).values('facebookid')
    i = fb_friends_found[0] #To get the dictionary inside of the list
    results = i['facebookid'] #To retrieve the value for the 'facebookid' key
    variables = RequestContext (request, {'results': results })
    return render_to_response('findfriends.html', variables)

I carried out the first three lines within the 'try' block using manage.py shell and this worked fine, printing the correct 'facebookid'. Unfortunately I can't get it to work in my browser. Any suggestions?

Leon
  • 1

1 Answers1

0

Do you have a specific problem you're running into, such as an exception?

I feel like you should get some kind of exception if you have a try block without an except statement.

try:
   # something
except Exception: # but be more specific
   print "exception occurred"

Otherwise, the code looks good, and if nothing is rendering in your browser, I'd look into the template. Unless... you're hiding errors in your try block, in which case you should remove the try block and let the error occur to understand what's wrong.

Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
  • Thanks for your answer... I don't get any error or exception. The view is triggered by a button click. When I click the button nothing is returned. However when I add literal text instead of the variable: i = fb_friends_found[0] it works fine. For some reason it doesn't like: i = fb_friends_found[0] – Leon Mar 08 '11 at 20:20
  • So your try block is not masking a problem? The view is rendered but with nothing for results? I'd personally start putting in log statements into your code, either `print fb_friends_found` or log. – Yuji 'Tomita' Tomita Mar 08 '11 at 21:35
  • Thanks Yuji. I solved the issue ... the problem was the I was the variable 'f' as though it was a list - comparing it to 'facebookid'- when in fact is was a string. I simply used f.split(',') and that solved the problem! Thanks for your help! – Leon Mar 13 '11 at 13:38
  • Then you should have gotten an exception about that -- definitely stay away from agressive try/excepts ! – Yuji 'Tomita' Tomita Mar 13 '11 at 16:09