Reputation: 470
I am trying to add a simple RSS parser to my web application. The objective is to grab an RSS channel and display news from it on a single page. I managed to do this for a single object, but can't do it for many objects (say, 10).
The project assumes I have the files views.py
and RSS.html
.
The following IS code which is working for one single parse object.
views.py
:
import feedparser
def rss(request):
feeds = feedparser.parse("https://www.informationweek.com/rss_simple.asp")
entry = feeds.entries[0]
return render(
request,
'posts/rss.html',
feeds={
'title': entry.title,
'published': entry.published,
'summary': entry.summary,
'link': entry.link,
'image':entry.media_content[0]['url']
}
)
RSS.html
:
<h3>{{ title }}</h3>
<i>Date: {{ published }}<p></i>
<b>Summary:</b><p> {{ summary }}<p>
<b>Link:</b><a href="{{ link }}"> {{ link }}</a><p>
<b>Image:</b><p><img src="{{ image }}"></img><p>
I don't understand how to pass all feeds into RSS.html.
I try to pass it through views and it doesn't work.
The following is code that does NOT work:
views.py
:
return render(request, 'posts/rss.html', feeds)
RSS.html
{% for entry in feeds %}
<li><a href="{{entry.link}}">{{entry.title}}</a></li>
{% endfor %}
Upvotes: 0
Views: 1374
Reputation: 9405
When passing the feed object into the template, you have to loop over the entries
field of the feed object:
Python:
import feedparser
def rss(request):
feed = feedparser.parse("https://www.informationweek.com/rss_simple.asp")
return render(request, 'posts/rss.html', {'feed': feed})
HTML:
{% for entry in feed.entries %}
<li><a href="{{entry.link}}">{{entry.title}}</a></li>
{% endfor %}
General documentation:
The entries documentation:
A list of dictionaries. Each dictionary contains data from a different entry. Entries are listed in the order in which they appear in the original feed.
Upvotes: 1