Building a News App with Django and Newsapi

Jan 18, 2022·

3 min read

Play this article

This article will show how we can build a simple news app using Django and News API. The project is easy and a great beginner project to Django and python.

We will use newsapi to show the latest news of different category. We can fetch the top headlines from different sources and this project will use Django to get the news headline along with the link and display it so that we can click the link to read the full story.

1/ Wake sure to have a new folder to store this project. We need to set up an an environment so that we can install the packages for our project in it. Run the following commands in terminal inside the folder we have for our project.

$ python -m venv env
$ env\Scripts\activate

2/ Install django and newsapi packages

$ pip install django
$ pip install newsapi-python

3/ Now we will create our project:

$ django-admin startproject news

4/ cd into the news directory. Create the newsapp using the command:

$ python startapp newsapp

5/ Try to run the local server to check if it is running:

$ python runserver

This will run our app at, check it in the browser.

6/ Get the apiKey from We can use this key to retrieve news data.

7/ Create the views for the app by opening the file. In the views, we will define a function that requests for the web response. The response will be a json file, so we need to extract the required data from it and render it to a html file. The code will look similar to:

from django.shortcuts import render
from newsapi import NewsApiClient 

# Create your views here.
def home(request):
    newsapi = NewsApiClient(api_key='yourapikey')
    topnews = newsapi.get_top_headlines('cnn')   # source=ndtv, bbc-news, cnn,techcrunch,foxnews.

    latest = topnews['articles']
    title = []
    desc = []
    url = []
    author = []
    date = []

    for i in range(len(latest)):
        news = latest[i]


    all_news = zip(title, desc, url, author, date)

    context = {
        'all_news': all_news

    return render(request, "index.html", context)

In the above, we first imported the libraries and then we defined a function called home which takes in the requests. The function fetches the top headlines from the source 'cnn' and then renders the html template.

  • We passed the context that stores the news data.

8/ Create a HTML template.

  • Create a template folder inside the newsapp directory.
  • inside the template folder, create a new file "index.html"
<!DOCTYPE html>
<html lang="en">

    <meta charset="utf-8">
    <title>News for you</title>

        body {
            margin: 0rem 20rem;

        a {
            text-decoration: none;

        .header {
            margin-top: 5px;
            width: 100%;
            border-radius: 5px;
            padding: 10px;
            background: darkgrey;
            text-align: center;
        .container {
            margin: 0;
            padding: 10px;

        .list {
            margin: 8px 5px;
            border: 1px solid #111111c9;
            padding: 10px;
    <link rel="stylesheet" href="">

    <div class="header">
        <h1><a href="{% url 'home' %}">What's the News?</a></h1>

    <div class="container">
        {% for title, description, url, author, date in all_news %}

        <div class="list">
            <a href="{{ url }}">
                <h4 class="post-title">{{ title }}</h2>
            <p class="post-subtitle">{{ description }}</p>

            <p>Posted by <a href="#">{{ author }}</a> on {{ date }}</p>

        <!-- Divider-->

        {% endfor %}



9/ Define a path for our view. Go to file and mention the path to the view.

from django.contrib import admin
from django.urls import path
from newsapp import views

urlpatterns = [
    path('', views.home, name ='home'),


Its finally done, try to run the server using

$ python runserver


Screenshot 2022-01-20 at 16-04-45 News for you.png


Check the app hosted on heroku