'QuerySet' object has no attribute 'url' when using feedparser in Django

夙愿已清 提交于 2019-12-12 01:24:51

问题


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

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)

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

thanks


回答1:


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)


来源:https://stackoverflow.com/questions/19078710/queryset-object-has-no-attribute-url-when-using-feedparser-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!