Django - 将过滤结果传递给模板

发布于 2024-10-21 07:17:51 字数 569 浏览 2 评论 0原文

在我的 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)

我使用 manage.py shell 执行了“try”块中的前三行,效果很好,打印正确的“facebookid”。 不幸的是我无法让它在我的浏览器中工作。有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巾帼英雄 2024-10-28 07:17:51

您是否遇到了特定问题,例如异常?

我觉得如果你有一个没有 except 语句的 try 块,你应该得到某种异常。

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

否则,代码看起来不错,如果浏览器中没有呈现任何内容,我会查看模板。除非...您在 try 块中隐藏了错误,在这种情况下,您应该删除 try 块并让错误发生以了解问题所在。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文