0

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

Community
  • 1
  • 1
sikor
  • 143
  • 2
  • 10

1 Answers1

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)
justinas
  • 6,287
  • 3
  • 26
  • 36
  • OK, so now I have this in #views `def feed5(request): source = Feed.objects.all() for item in source: rss = feedparser.parse(item.url) context = {'rss': rss} return render(request, 'feedreader/feed5.html', context)` ##feed5.html looks like this `

    {{ rss.feed.title }}

      {% for r in rss.entries|slice:":15" %}
    • {{ r.title }}
      {{ r.description|striptags }}
    • {% endfor %}
    ` but the page displays rss entries only from one source and ignores skips all others from DB. Why?
    – sikor Sep 29 '13 at 14:21
  • You're only passing the last item into the context, as it is the last assignment. It doesn't matter that you iterate over everything. Instead, build up the data in some list (`li.append(rss)`) in the loop), pass the whole list in the context and iterate over the items of all of the feeds in your template (you'll need nested for-s). – justinas Sep 29 '13 at 16:20
  • Got it, thanks! `def feed5(request): source = Feed.objects.all() list = [] for item in source: rss = feedparser.parse(item.url) list.append(rss) context = {'rss': list} return render(request, 'feedreader/feed5.html', context)` – sikor Sep 29 '13 at 16:49