-1

I've got a function that is creating a list:

import feedparser
import ssl

def rss(self):
    if hasattr(ssl, '_create_unverified_context'):
        ssl._create_default_https_context = ssl._create_unverified_context
    rss = 'https://news.google.com/news?q=fashion&output=rss'
    feed = feedparser.parse(rss)
    articles = []
    for entry in feed.entries:
        articles.append({
                    "summary"    : str(entry.summary),
                    "link" : str(entry.link),
                    "title"   : str(entry.title),
        })

    return articles

on the return of articles, I am then trying to display this in the view

My views.py code is:

def results(request):
    rss_feed = RSS()
    articles = rss_feed.rss()
    return render(request, 'app/index.html', articles)

and then my template code is:

<ul>
{% for article in articles %}
    <li>{{ article['title'] }}</li>
{% empty %}
    <li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>

but I keep getting an error that reads:

TemplateSyntaxError at /app/results Could not parse the remainder: '['title']' from 'article['title']'

I created a separate script and just print to console and it prints without issue. But doesn't show in Django.

I'm new to Python and Django. Not sure what I've missed.

Update: Using {{ article.title }} returns an error context must be a dict rather than list.

kevinabraham
  • 1,378
  • 4
  • 28
  • 55

1 Answers1

2

You were passing the wrong context to the return function as you passed in your list directly to render. You have to pass your article list as a member of the context dictionary like this:

return render(request, 'app/index.html', {"articles": articles})

After this you can address the title of article as

{{ article.title }}

in your template.

Claude
  • 206
  • 1
  • 9