← Back to Blog

How to fetch medium articles/stories automatically to your django website

Jordan Mungujakisa
5 min read

Django Medium RSS Integration

Photo by Howard Bouchevereau on Unsplash

I have been working on building my portfolio website using Django as the backend and the front end is HTML and Bootstrap CSS. I needed to populate the recent blogs section on my portfolio because I write quite a lot on Medium and I really wanted to showcase that on my portfolio and the challenge was to automatically fetch my medium blog articles to display on my portfolio site.

So the first thing I had to do was check if Medium supported RSS feeds for their articles and luckily for me they did. Medium supports the use of RSS feeds for profiles, publications and topics.

You can read more: https://help.medium.com/hc/en-us/articles/214874118-Using-RSS-feeds-of-profiles-publications-and-topics

Please note that stories behind the Medium paywall are not available as full articles in any of the RSS feeds.

For my usecase, I wanted to automatically fetch my articles from medium to django. The feed for each profile can be retrieved using the following URL schema: medium.com/feed/@username or username.medium.com/feed

In my case, the URL to fetch my feed was https://medium.com/@jordan-mungujakisa/feed

Here is a step-by-step walkthrough of how I achieved my goal

Step 1: Install feedparser

Install the feedparser by running the following command in your terminal

pip install feedparser

Step 2: Set up the RSS URL

Get the URL to your feed and save it as a variable in your view.

rss_url = 'https://example.com/feed'

Step 3: Parse the feed

In your view, get the parsed feed using the feedparser library by adding the following code

feed = feedparser.parse(rss_url)

Step 4: Extract data from feed entries

We can then iterate over the feed and extract the fields we need to use in our site

for entry in feed.entries:
   title = entry.title,
   link = entry.link

Then you can create an object from the fields, create a list of them and pass it as part of the HTML context.

Complete Django View Implementation

Here is how your view should look like after adding the above code

def rss_feed(request):
    # Parse the RSS feed
    rss_url = 'https://example.com/feed'
    feed = feedparser.parse(rss_url)

    # Extract relevant information from the feed entries
    posts = []
    for entry in feed.entries:
        post = {
            'title': entry.title,
            'link': entry.link,
            'published': entry.published_parsed
        }
        posts.append(post)

    # Sort the posts by published date (newest first)
    posts.sort(key=lambda x: x['published'], reverse=True)

    return render(request, 'rss_feed.html', {'posts': posts})

Template Implementation

Then you can iterate over the posts in your django template and present the posts in your HTML

<div class="card-group gx-5">
      {% for post in posts %}
          <div class="card">
              <img src="{{ post.image }}" alt="{{ post.image }}" class="card-img-top">
              <div class="card-body">
                  <h5 class="card-title">{{ post.title }}</h5>
                  <p class="card-text h6"><small class="text-muted h6">{{ post.published }}</small></p>
              </div>
           </div>
      {% endfor %}
</div>

I hope you found this post useful, if so please follow me to be updated when I publish new articles.

You can also follow me on:

Thanks for reading my article!

Tschüss 👋


Originally published in Django Unleashed

Mobile app alchemist who is trying to transmute elegant designs, into elegant code, into beautiful mobile app experiences.

0 Comments

No comments yet. Be the first to share your thoughts!