Site running fine on local host but wont run on Heroku.

I keep getting this Server 500 error while deploying my to Heroku. It runs perfectly on my Local host. The other page on the site works fine but this one has the News-API. Not sure what to do next.

Debug ERROR

\`KeyError at /
'articles'
Request Method: GET
Request URL:    https://lvl27-9617c0ce663b.herokuapp.com/
Django Version: 4.2.10
Exception Type: KeyError
Exception Value:    
'articles'
Exception Location: /app/news/views.py, line 13, in index
Raised during:  news.views.index
Python Executable:  /app/.heroku/python/bin/python
Python Version: 3.9.18
Python Path:    
\['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python39.zip',
'/app/.heroku/python/lib/python3.9',
'/app/.heroku/python/lib/python3.9/lib-dynload',
'/app/.heroku/python/lib/python3.9/site-packages'\]
Server time:    Tue, 19 Mar 2024 23:03:02 +0000
Traceback Switch to copy-and-paste view
/app/.heroku/python/lib/python3.9/site-packages/django/core/handlers/exception.py, line 55, in inner
response = get_response(request) …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/handlers/base.py, line 197, in \_get_response
response = wrapped_callback(request, \*callback_args, \*\*callback_kwargs) …
Local vars
/app/news/views.py, line 13, in index

a = gaming_news\['articles'\]\`

CODE

from django.shortcuts import render
import requests
import os
from dotenv import load_dotenv, dotenv_values
from newsapi import NewsApiClient

load_dotenv()
API = os.getenv('NEWS_API')

def index(request):
    url = (f"https://newsapi.org/v2/everything?q=gaming&apiKey={API}")
    gaming_news = requests.get(url).json()
    a = gaming_news['articles']

    urlToImage = []
    author = []
    title = []
    description = []
    url = []

    for i in range(len(a)):
        f = a[i]
        urlToImage.append(f['urlToImage'])
        author.append(f['author'])
        title.append(f['title'])
        description.append(f['description'])
        url.append(f['url'])
    news_list = zip(urlToImage, author, title, description, url)
    
    context = {'news_list': news_list}

    return render(request, 'news/news.html', context)

I tried change up the code but I’ve been at it way too long already.

1 Like

This error indicates that the key ‘articles’ doesn’t exist in the response JSON returned by the News API. May be an issue with your API key or incorrect URL construction, or a problem with the API itself. use try and except to identify the main issue.