[Answered ]-How to Output More than Once In frontend In DJango

1👍

You’re only getting one result because your return statement is calling "submission_dict". You probably wanted to call "submission_dict(s)" or something similar but it’s hard to tell based on what has been commented out in your code. I think that’s your primary problem. Without seeing your template, I don’t know what your context object is supposed to look like.

But try this:

# views.py
def hacker_news_api_selector():
    url = 'https://hacker-news.firebaseio.com/v0/topstories.json' 
    r = requests.get(url)
    submission_list = r.json()
    context = {}
    context['objects'] = []

    # Process information about each submission.
    for submission_id in submission_list[:10]:

        # Make a separate API call for each submission.
        url = f"https://hacker-news.firebaseio.com/v0/item/{submission_id}.json" 
        r = requests.get(url)
        response_dict = r.json()
        context['objects'].append(response_dict)

    return context 

def home(request):
    context = hacker_news_api_selector()
    return render(request, "news_api/home.html", context)

And in the template…

# news_api/home.html
<html>
    ...
    {% for x in objects %}
        <li>{{ x.title }}</li>
    {% endfor %}
</html>

Leave a comment