2011-03-08 27 views
0

在我的Django的看法,我正在嘗試從我的數據庫中的結果,然後把它們交給我的模板用下面的代碼:Django的 - 傳遞一個濾波的結果模板

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) 

我進行的在'try'塊中使用manage.py shell的前三行很好,打印正確的'facebookid'。 不幸的是我無法讓它在我的瀏覽器中工作。有什麼建議麼?

回答

0

你有沒有遇到特定的問題,例如異常?

我覺得你應該得到某種異常,如果你有一個沒有except語句的try塊。

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

否則,代碼看起來不錯,如果你的瀏覽器沒有渲染,我會查看模板。除非......你在你的try塊中隱藏了錯誤,在這種情況下,你應該刪除try塊,並讓錯誤發生以理解錯誤。

+0

感謝您的回答......我沒有得到任何錯誤或異常。該視圖由點擊按鈕觸發。當我點擊按鈕時沒有返回。但是,當我添加文字而不是變量:i = fb_friends_found [0]它工作正常。由於某種原因,它不喜歡:我= fb_friends_found [0] – Leon 2011-03-08 20:20:16

+0

所以你的嘗試塊不掩蓋一個問題?視圖呈現,但沒有結果?我個人會開始在日誌中放入代碼,可以是'print fb_friends_found'或log。 – 2011-03-08 21:35:56

+0

謝謝Yuji。我解決了這個問題......問題在於我是變量'f',好像它是一個列表 - 當它實際上是一個字符串時,將它與'facebookid'進行比較。我只是使用f.split(',')並解決了問題!謝謝你的幫助! – Leon 2011-03-13 13:38:52

相關問題