Make RSS reader using python and django : Mini Project for computer science

RSS reader using python and django. Mini Project idea for computer science and IT students


RSS stands for Real Simple Syndication or Rich Site Summary. Here we are going to make simple RSS reader using python and django framework. Computer science or IT students can make a mini project on this.

What are we going to do exactly?

We will create a web page interface that accepts external RSS url and gives output on a web page in a very simple manner. The output will be headlines of the news, small summary or description of that news and link to the specific news.

For RSS reader, we are going to use a powerful backend python framework - Django. In the frontend, we are going to use simple cdn bootstrapping.

I'm using VS code as a code edit for this tutorial. You are free to use any of your favorite code editors.

Let's create a folder and name it as a RSSReader (You can name it anything). Now open your code editor and open a created folder in the code editor. Create a virtual environment to keep project files isolated from your other computer files. For this, type following command in the terminal:

python -m venv mysite

Now activate the virtual environment using the following command:

mysite\Scripts\activate

Let's install django framework using the following command:

pip install django

Also, install the feedparser python library for parsing RSS feeds:

pip install feedparser

Now create a project in django with the name "rssreader"

django-admin startproject rssreader .

Create a new app with the name "rss" using:

python manage.py startapp rss

Go to the settings.py file and add app name 'rss'  in the INSTALLED_APPS =[] as shown below:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rss',
]

Create a "templates" folder inside the rss folder like "rss/templates". Again create a folder "rss" folder inside the templates folder. It should look like: "rss/templates/rss" . Now create reader.html and base.html files this rss folder.

In reader.html, type the following code:

reader.html
{% extends 'rss/base.html' %}

{% block body %}
<br />
<form class="form-inline">
<div class="form-group mx-sm-3 mb-2">
<label for="inputPassword2" class="sr-only">Password</label>
<input type="text" class="form-control" placeholder="paste link"
name="url">
</div>
<button type="submit" class="btn btn-primary mb-2">Search</button>
</form>
{% if feed %}
<h2>{{ feed.feed.title }}</h2>
{% if feed.entries %}
{% for entry in feed.entries %}
<div class="card">
<div class="card-header">
Feed
</div>
<div class="card-body">
<h5 class="card-title">Headline: {{ entry.title }}</h5>
<p class="card-text">Description: {{ entry.description }}</p>
<a href="{{ entry.link }}" class="btn btn-primary">Visit Link</a>
</div>
</div><p>{{ entry.published }}</p>

{% endfor %}
{% endif %}
{% else %}
<br />
<p>Enter your favorite RSS feed above.</p>
{% endif %}

{{ feed2 }}

{% endblock %}

In base.html type the following code:

base.html
<!DOCTYPE html>
<head>
<title>Django RSS Reader</title>
</head>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="/">Home</a>
</ul>
</nav>
<body>
{% block body %}{% endblock %}
</body>
</html>

For cdn bootstrapping, add below lines before </head> in base.html

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">

We are done with the template formatting. Now to go views.py file in the rss app folder and type the following code in it. Remove all previous code.

Views.py
from django.shortcuts import render
from django.http import HttpResponse
import feedparser


def index(request):
if request.GET.get("url"):
url = request.GET["url"] #Getting URL
feed = feedparser.parse(url) #Parsing XML data
else:
feed = None
return render(request, 'rss/reader.html', {
'feed' : feed,
})

Now open urls.py file from main project folder "rssreader". Its location should look like "rssreader/urls.py" . Remove all previous code from the file and type following code:

rssreader/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('rss.urls')),
]

Its time to create url route for your app. For this, create urls.py file in rss app folder. Its location should look like "rss/urls.py". Type the following code in that file. Don't forget to remove default code from the file.

rss/urls.py
from django.urls import path, include
from . import views

urlpatterns= [
path('', views.index, name='index')
]

Its time to run our RSS reader django project in the browser. Type the following command in the terminal:

python manage.py runserver

After its successful execution in the terminal open any web browser and go to this address http://127.0.0.1:8000/  Here you can see your RSS reader webpage.

For testing, type RSS feed url of  BBC news website in the search box

URL : http://feeds.bbci.co.uk/news/rss.xml

Now you can see live news feeds from the BBC website on your RSS reader webpage.

You can see the screenshot of  RSS reader webpage:


RSS reader using python and django : Mini Project for computer science
RSS reader using python and django


If you are facing any problem regarding " RSS reader using python and django " then feel free to comment below. I will try my best to solve your problem


Post a Comment

0 Comments