How I Designed a News Aggregator Web Application Using Django

Rate this post

A news aggregator is simply a web application that aggregates or collects news articles from different websites and presents them in a single location so that users can visit the location (website) and click on the link to read news of interest. This saves time and resources.

In this project article, we will learn how to design a news aggregator web application using the Django framework. Specifically, we will scrape the web for news articles using a Python module, and have them displayed on our Django web application.

This is something similar to the News App designed using the Flask framework in previous tutorial projects.

Setting up Django

Follow these steps to set up Django in your Ubuntu terminal:

$ mkdir project
$ cd project
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ pip install django requests bs4
(.venv) $ django-admin startproject newsAggregator .
(.venv) $ python3 startapp news
(.venv) $ python3 migrate
(.venv) $ python3 runserver

Remember, the Django project is created in the project folder as indicated by the dot command. Run the local server to confirm that the installation went successfully.

Creating Views

from django.shortcuts import render
import requests

from decouple import config

API_KEY = config('NEWS_API_KEY')

COUNTRY = 'us'

def news_lists(request):
    if request.method == 'POST':
        url = f'{COUNTRY}&apiKey={API_KEY}'
        res = requests.get(url).json()
        news_articles = res['articles']
        context = {
            'news_articles': news_articles
        return render(request, 'home.html', context)
    return render(request, 'home.html'

We import the requests module which is an essential tool for retrieving data from a webpage. We use the Python-decouple module to load environment variables. The first variable is the News API key. If you have read the project tutorial where I used the Flask framework to create a similar project, you are expected to have your API key.

Create a .env file to save your environment variables. Inside the file, write this:

API_KEY = 'Your API key'

The Second environment variable is Django’s secret key found in the file. The instruction from Django is to keep the secret key secret.

Of course, this applies to deploying an app to a production server. But since I will be pushing the files to GitHub, I have to follow best practices by keeping it secret and loading it as an environment variable.

Another Python module, Python-dotenv is working behind the scene to make all these possible. If you start the server without installing the module, you may get an error message from Django.

Back to the view function, if the request method is POST, we use the requests module to fetch current news from a defined country. You can change it to use any country of your choice. We then save the response in a dictionary form and render it to the home.html webpage.

Creating Templates

We will create a home.html file inside a templates folder. So, create the folder and the file.

<!DOCTYPE html>
<html lang="en">
    <!-- Required meta tags -->
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- Bootstrap CSS -->
    <title>Django News Headline</title>
    <h1 class="text-center mt-4 mb-5">Django News Headlines</h1>
    <div class="row mt-5">
      <div class="col-md-2"></div>
      <div class="col-md-8">
        <div class="row">
          {% for news in news_articles %}
          <div class="col-md-4 mb-5">
            <div class="card" style="width: 18rem">
              <img src="{{ news.urlToImage }}" class="card-img-top" alt="..." />
              <div class="card-body">
                <h5 class="card-title">{{ news.title }}</h5>
                <p class="card-text">{{ news.description }}</p>
                <a href="{{ news.url }}" class="btn btn-primary">Read More</a>
          {% endfor %}
      <div class="col-md-2"></div>
    <!-- Bootstrap Bundle with Popper -->
    <script src=""

Going back to the view function, you will notice the variable news_articles.

When the response was converted to JSON, the news content was found in res['articles']. Having stored it in the news_articles, we simply use Django’s for-loop to iterate through the articles. This is also similar to what we did using the Flask framework.

Each iteration displays the news title, image, description, and a link to read more

Registering Apps and URLs

Go to the file. If you will be pushing your code to GitHub, remove the Django secret key, store it in the .env file, and load it using the Python-decouple module.

from pathlib import Path
import os
from decouple import config


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See

# SECURITY WARNING: keep the secret key used in production secret!

Scroll down to the INSTALLED_APPS section, and register the news app.


Scroll down to the TEMPLATES section, and also let Django know of an existing templates folder.

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')], #add these
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [

Next is the URLs. Go to the project-level file.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('news.urls')),

This also informs Django of an existing app-level URLs. Finally, let’s create the app-level file. This time, inside the news folder.

from django.urls import path
from .views import news_lists

urlpatterns = [
    path('', news_lists, name='home'),

The news_lists() is our view function. The URL will be referred to as home in templates files where necessary.

Everything is done and dusted. Let’s start the local server. Run python3 runserver in your terminal.


Having gone through this project, no doubt, you will appreciate the simplicity Flask has compared with Django.

However, Django can be used to create web apps far more complex than the Flask framework. What we just did with Django is for learning’s sake, to improve our Python skills. You have learned a thing or two.

Use that knowledge to create a similar app and share it with the world!

💡 Recommended: How I created a News Application using the Flask Framework