Final Application

See how News API can be used in an actual application.

We'll cover the following

Let’s now look at a Django application that uses some of the News API endpoints to retrieve the requested articles.

Resources used

We’ll use the everything and the top-headlines endpoints in this application. The language and sortBy parameters will be used with the everything endpoint, whereas country and category parameters will be used with the top-headlines endpoint.

Live demo

The widget below contains the code for our application. Click the “Run” button to run the application.

Bud1it__.p__init__.pyIlocblobA.������asgi.pyIlocblob�.������settings.pyIlocblob.������urls.pyIlocblob�.������wsgi.pyIlocblobg.������ @� @� @� @EDSDB `� @� @� @
News application

Let’s look at the code in views.py, where the API is being called:

  • The data entered by the user is retrieved in lines 9 to 12.
  • Depending on whether everything or top-headlines has been selected, the url on line 18 or line 21 will be selected.
  • We finally make the API call on line 23 and save the response in the response variable.
  • This data is saved in the context variable and returned on line 43. It is displayed using the index.html file.

You can use this application to search the news for a specific country or category. All the news will be retrieved using News API.