在Django中使用feedparser时,“QuerySet”对象没有属性“url” [英] 'QuerySet' object has no attribute 'url' when using feedparser in Django

查看:316
本文介绍了在Django中使用feedparser时,“QuerySet”对象没有属性“url”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从这里的问题的后续 Django / feedparser中的bozo_exception

This is follow up to the question from here bozo_exception in Django / feedparser

我想通过模型/数据库中的许多Feed进行迭代,并将其中的每一个都显示在html模板中。虽然我明白我需要在html模板中迭代思考x.feed.entries,我认为通过每个rss源的迭代需要在视图函数中正确执行?

I would like to iterate through many feeds from models/DB and have each of them displayed in the html template. While I do understand that I need to iterate thought x.feed.entries in the html template, I assume that iteration through each rss source needs to happen in the view function correct?

def feed5(request):
    source = Feed.objects.all()
    for item in source.url:
        rss = feedparser.parse(item)
    context = {'rss': rss,}
    return render(request, 'feedreader/feed5.html', context)

给我这个错误:'QuerySet'对象没有属性'url'。不确定我该怎么办?

gives me this error: 'QuerySet' object has no attribute 'url'. Not sure how should I go about it?

谢谢

推荐答案

那么它实际上并不是 - Python不是对你说谎。请参阅 source 是一个 QuerySet ,一个类似列表的结果结果,而不是一个结果。如果是您的 Feed 模型,应该具有一个url属性,然后查找它,而不是查询集:

Well, it actually doesn't - Python isn't lying to you. See, source is a QuerySet, a list-like structure of results, not a single result. If it's your Feed model that should have a url attribute, then look it up on it and not the query set:

for item in source:
    rss = feedparser.parse(item.url)

这篇关于在Django中使用feedparser时,“QuerySet”对象没有属性“url”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆