In this tutorial, we will learn how to build a news aggregation website using Python and the Django web framework. A news aggregation website collects news articles from various sources and presents them in a single place for users to read. By the end of this tutorial, you will have a functioning news aggregation website that can fetch news articles from external sources and display them to users.
Before starting this tutorial, you should have a basic understanding of Python programming language and web development concepts. Familiarity with HTML, CSS, and database concepts will be beneficial but not mandatory. Additionally, you need to have Python and Django installed on your system.
To set up your development environment, follow these steps:
pip install Django
django-admin --version
You should see the version number of Django printed on the terminal.
With the setup complete, we can now proceed to build our news aggregation website.
Before we begin building our website, let’s confirm that Django is installed correctly. Open your terminal or command prompt and run the following command:
django-admin --version
If you see the version number of Django printed on the terminal, it means Django is installed correctly.
To get started, let’s create a new Django project. Open your terminal or command prompt and navigate to the desired directory where you want to create your project. Then, run the following command:
django-admin startproject news_aggregator
This command creates a new folder named news_aggregator
and generates the basic structure of a Django project inside it.
By default, Django uses a lightweight SQLite database for development purposes. However, you can also use other databases like PostgreSQL, MySQL, etc. In this tutorial, we will stick with SQLite for simplicity.
Open the settings.py
file inside the news_aggregator
folder and locate the DATABASES
section. Replace the existing configuration with the following code:
python
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Django projects are organized using apps. Each app represents a specific functionality of the website. Let’s create a new app called news
using the following command:
python manage.py startapp news
This command creates a new news
folder inside the project directory.
Models define the structure of the data stored in the database. Open the models.py
file inside the news
folder and define the following models:
```python
from django.db import models
class Source(models.Model):
name = models.CharField(max_length=255)
url = models.URLField()
def __str__(self):
return self.name
class Article(models.Model):
title = models.CharField(max_length=255)
description = models.TextField()
url = models.URLField()
source = models.ForeignKey(Source, on_delete=models.CASCADE)
def __str__(self):
return self.title
``` These models represent the source of a news article and the article itself. The `Source` model has a name and a URL, while the `Article` model has a title, description, URL, and a foreign key to the `Source` model.
Views handle the logic behind the website’s functionality. Open the views.py
file inside the news
folder and define the following views:
```python
from django.shortcuts import render
from .models import Article
def index(request):
articles = Article.objects.all()
return render(request, 'news/index.html', {'articles': articles})
``` This view retrieves all the articles from the database and passes them to a template called `index.html`.
Templates define the structure and layout of the website’s pages. Create a new folder called templates
inside the news
folder. Inside the templates
folder, create another folder called news
. Then, create a new file called index.html
inside the news
folder. Add the following code to the index.html
file:
```html
{% for article in articles %}
<h2>{{ article.title }}</h2>
<p>{{ article.description }}</p>
<a href="{{ article.url }}" target="_blank">Read more</a>
{% empty %}
<p>No articles available.</p>
{% endfor %}
``` This template iterates over the list of articles passed from the view and displays their title, description, and a link to read the full article.
URLs map the website’s URLs to their corresponding views. Open the urls.py
file inside the news_aggregator
folder and add the following code:
```python
from django.urls import path
from news.views import index
urlpatterns = [
path('', index, name='index'),
]
``` ### Step 9: Run the Development Server
To test the website, run the following command in your terminal or command prompt:
python manage.py runserver
Open your web browser and go to http://localhost:8000
to see the news aggregation website. It should display the list of news articles stored in the database.
Now that we have a basic website, let’s add functionality to fetch news articles from external sources. For this tutorial, we will use the News API to retrieve news articles. Sign up for a free API key from the News API website and replace <YOUR_API_KEY>
in the following code with your actual API key:
```python
import requests
from .models import Source, Article
def fetch_articles(request):
response = requests.get('https://newsapi.org/v2/top-headlines?country=us&apiKey=<YOUR_API_KEY>')
data = response.json()
sources = data['sources']
for source_data in sources:
source, _ = Source.objects.get_or_create(
name=source_data['name'],
url=source_data['url']
)
articles_data = source_data['articles']
for article_data in articles_data:
Article.objects.get_or_create(
title=article_data['title'],
description=article_data['description'],
url=article_data['url'],
source=source
)
return redirect('index')
``` This view sends a request to the News API to fetch the top headlines in the US. It then parses the response and saves the articles and source objects into the database.
To keep the news articles up to date, we can schedule periodic fetching using Django’s django-crontab
library. Install the library by running the following command:
pip install django-crontab
Open the settings.py
file inside the news_aggregator
folder and add the following code at the bottom:
```python
from crontab import CronTab
CRONJOBS = [
('*/15 * * * *', 'news.views.fetch_articles', '>> /tmp/news_aggregator.log')
]
cron = CronTab(user='<your_username>')
for (schedule, command, output) in CRONJOBS:
job = cron.new(command=command, user=cron.user, comment='news_aggregator')
job.setall(schedule)
cron.write()
``` Replace `<your_username>` with your actual username. This code sets up a cron job that executes the `fetch_articles` view every 15 minutes and redirects the output to a log file.
That’s it! You have successfully built a news aggregation website with Django. Now, whenever the cron job runs, new articles will be fetched and displayed on your website.
In this tutorial, we learned how to build a news aggregation website using Python and the Django web framework. We covered the following steps:
By following this tutorial, you have gained knowledge and hands-on experience in Python web development with Django. You can further enhance your website by adding features like user authentication, search functionality, or advanced filtering options.