0

I would like to search different items (jobs, stories, ask) from the hacker news api but I can't seem to figure out how to do it correctly, please check code below and tell me what I'm doing wrong as I'm unable to run it successfully.

def search(request):
    if 'search' in request.GET:
        search = request.GET['search']
        url = 'https://hacker-news.firebaseio.com/v0/item/{item-id}.json?print=pretty'
        response = requests.get(url)
        article_list = response.json()
        context = {}
        context['objects'] = []

        for each_id in article_list[:10]:
        # Make a separate API call for each article.
            url = f"https://hacker-news.firebaseio.com/v0/item/{each_id}.json" 
            # get response for individual articles
            response = requests.get(url)
            article_dict = response.json()
            context['objects'].append(article_dict)
    return render(request, 'SyncNews/search.html', context)
    {% for x in objects %} 
        <h3 class="news-subheading">{{ x.title }}</h3> 
    {% endfor %}
                <form method="GET" action="{% url 'search' %}">  
                    <input type="text" name="item-id" placeholder="Search" />
                </form> 
Chuks
  • 59
  • 8

1 Answers1

0

Your code in line 4 is problematic.

url = 'https://hacker-news.firebaseio.com/v0/item/{item-id}.json?print=pretty'

{item-id} has not been defined.

Adetoro Lanre
  • 43
  • 1
  • 4