Building a News Aggregator with Django


Introduction

A news aggregator is a valuable tool for collecting and presenting news and articles from various sources. In this guide, we'll walk you through the process of building a news aggregator with Django, a popular web framework.


1. Project Setup

Start by creating a new Django project and app for your news aggregator. Use the following commands to set up your project and app:


# Create a new Django project
django-admin startproject news_aggregator_project
# Create a new app for the aggregator
python manage.py startapp news_aggregator

2. Models for Storing News Sources and Articles

Define models to store news sources and articles. You can create models like

Source
and
Article
in your app's
models.py
file. Here's a basic example:


# news_aggregator/models.py
from django.db import models
class Source(models.Model):
name = models.CharField(max_length=100)
url = models.URLField()
def __str__(self):
return self.name
class Article(models.Model):
source = models.ForeignKey(Source, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
link = models.URLField()
publication_date = models.DateTimeField()
def __str__(self):
return self.title

3. Creating Views and Templates

Build views to display news sources and articles. Create HTML templates for rendering the content. You can use Django's template system to structure and present the data.


# news_aggregator/views.py
from django.shortcuts import render
from .models import Source, Article
def source_list(request):
sources = Source.objects.all()
return render(request, 'news_aggregator/source_list.html', {'sources': sources})
def article_list(request, source_id):
articles = Article.objects.filter(source_id=source_id)
return render(request, 'news_aggregator/article_list.html', {'articles': articles})

4. URL Configuration

Configure your app's URLs to map to the views you've created. Define URL patterns in your app's

urls.py
file.


# news_aggregator/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('sources/', views.source_list, name='source_list'),
path('articles/<int:source_id>/', views.article_list, name='article_list'),
]

5. Creating Templates

Create HTML templates for displaying news sources and articles. You can use Django's template system and include styling and layout as needed.


<!-- news_aggregator/templates/news_aggregator/source_list.html -->
{% extends "base.html" %}
{% block content %}
<h2>News Sources</h2>
<ul>
{% for source in sources %}
<li><a href="{% url 'article_list' source.id %}">{{ source.name }}</a></li>
{% empty %}
<li>No sources available.</li>
{% endfor %}
</ul>
{% endblock %}
<!-- news_aggregator/templates/news_aggregator/article_list.html -->
{% extends "base.html" %}
{% block content %}
<h2>Articles</h2>
<ul>
{% for article in articles %}
<li><a href="{{ article.link }}" target="_blank">{{ article.title }}</a></li>
{% empty %}
<li>No articles available for this source.</li>
{% endfor %}
</ul>
{% endblock %}

6. Running Migrations

Apply migrations to create the database tables for your models using the following command:


python manage.py makemigrations
python manage.py migrate

7. Admin Panel

To manage news sources and articles, you can use Django's admin panel. Register your models in your app's

admin.py
file to make them accessible.


# news_aggregator/admin.py
from django.contrib import admin
from .models import Source, Article
admin.site.register(Source)
admin.site.register(Article)

8. Scraping and Populating Data

To populate your news aggregator with data, you can implement web scraping or use APIs from news sources. Create management commands or scripts to periodically fetch and store news articles in your database.


Conclusion

Building a news aggregator with Django offers a flexible and scalable solution for collecting and presenting news from various sources. Customize and expand your aggregator to fit your specific requirements and provide a valuable resource for your users.