Chuks
Chuks

Reputation: 59

Unable to search from hacker news api django

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> 

Upvotes: 0

Views: 100

Answers (1)

Adetoro Lanre
Adetoro Lanre

Reputation: 61

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.

Upvotes: 0

Related Questions