Build a News App with Django and Python
Django projects for Beginners and Intermediates

Overview of the Project
News App using Django is a beginner and Intermediate level project. We are going to make this project in a virtual environment then fetch the news from News API. At last, we will do a little bit of styling.
The outcome of the Project

Our final result will look exactly like the above image.
If you are looking for a video tutorial then it’s here:
Project Setup and Installations
Attention all developers seeking to make social connections and establish themselves while earning passive income — look no further! I highly recommend ‘From Code to Connections’, a book that will guide you through the process. Don’t miss out, grab your copy now on Amazon worldwide or Amazon India! You can also go for Gumroad
Always try to start your Django project in a virtual environment. As a python developer, It’s a good habit to make projects in the virtual environment. If you don’t know how to start a Django project in a virtual environment then here is the link that may help you. It’s for both Windows and Linux users.
Open a directory of your choice and run the commands below.
virtualenv -p python3.8 news
cd news
source bin/activate
Now, in the same directory install Django and the other libraries that we are going to use in this project.
pip install django
pip install requests

Start a Django project in the same directory.
django-admin startproject NewsApp
cd NewsApp
python manage.py startapp MainApp
After you go through the above steps, your Django project will be ready.
NewsAPI
In this project, we are going to use the API of NewsAPI. You just have to make your account here. After that, you will get an API key. We are going to use that API Key to fetch news data. You can fetch trending headlines, world news, or topic-specific news from this API. Remember that, API key is not sharable. So, keep this KEY a secret.
We are done with the project setup and installation. Let’s switch to the code part.
Code of Weather App
I will provide the source code of this project at the end of this article. For now, let’s code step by step.
We are not storing any news in our database, as we are going to fetch it from the API. So, leave the models empty.
MainApp
views.py
from django.shortcuts import render
import requests# Create your views here.def index(request):url = 'https://newsapi.org/v2/everything?q=Cryptocurrency&from=2021-09-08&sortBy=popularity&apiKey=NEWS_API'crypto_news = requests.get(url).json()a = crypto_news['articles']
desc =[]
title =[]
img =[]for i in range(len(a)):
f = a[i]
title.append(f['title'])
desc.append(f['description'])
img.append(f['urlToImage'])
mylist = zip(title, desc, img)context = {'mylist': mylist}return render(request, 'index.html', context)
- Import the required libraries.
- Define an index function. In the function, Add your API key to the URL.
- We fetch the news in JSON format.
- If you look at the documentation of NEWS API, the news data is stored in the “articles” array. We are going to use that array. First, fetch it. Then define the information we need from the NEWS API and store that information in an array. We need a description, title, and image of the news in this project.
- Now, append the data in the respective array.
- Render the HTML template.
urls.py
Make a urls.py file inside the MainApp and add the following code.
from django.urls import path
from . import viewsurlpatterns = [
path('', views.index),
]
We are done with our MainApp.
Check out the template code in the Github Repo of this project. The link is down below.
NewsApp
urls.py
from django.contrib import admin
from django.urls import path, include #add
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('MainApp.urls')), #add
]
Add the URLs of the MainApp.
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles', 'MainApp' #add
]
Add your app inside the installed apps segment. We are good to go now.
Open your command prompt and run the following command:
python manage.py makemigrations
python manage.py migrate
python manage.py runserver

You will see something like this on your terminal or command prompt screen. Now open your browser and enter the given URL.
localhost:8000
Now, when you hit enter, you will see only cryptocurrency-related news on your browser screen. Because we set the query as cryptocurrency in the URL of views.py. Change the query to world news, soccer news, cricket news, top headlines, or anything specific field of news you want to know. You will get that news on your screen.
url = 'https://newsapi.org/v2/everything?q=Cryptocurrency&from=2021-09-08&sortBy=popularity&apiKey=NEWS_API'
The Github code of the weather app is here.
That’s it.
Thank you for reading. If this article is informative then make sure to follow and share it with your community and follow for more.
Check out more Django projects, the link is down below.
Make your First Web App with Django: Python in a Virtual Environment
Build A Weather App Using Django and Python
Build a To-Do List API using Django Rest framework
How to make Token-Based Auth Rest API using Django rest framework
Happy coding!
Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build react native projects and currently working on Python Django. Feel free to contact me at freelance.rohit7(at)gmail.com.