Creare un applicazione blog con django, parte 9: creare feed rss in django

Django ha un framework feed di sindacazione integrato che puoi usare per generare dinamicamente feed RSS o Atom in modo simile alla creazione di sitemap utilizzando il site framework. Un feed Web è un formato di dati (in genere XML) che fornisce agli utenti contenuti aggiornati di frequente. Gli utenti potranno iscriversi al feed utilizzando un aggregatore di feed, un software utilizzato per leggere i feed e ricevere nuove notifiche sui contenuti.

Crei un nuovo file nella directory dell'applicazione blog e nominarlo feeds.py. Aggiungi il codice seguente:

# blog/feeds.py

from django.contrib.syndication.views import Feed
from django.template.defaultfilters import truncatewords

from .models import Post

class LatestPostsFeed(Feed):
   title = 'My blog'
   link = '/blog/'
   description = 'Latest posts of my blog.'

   def items(self):
       return Post.published.all()[:3]

   def item_title(self, item):
       return item.title

   def item_description(self, item):
       return truncatewords(item.body, 30)

Innanzitutto, sottoclassiamo la classe Feed del framework di sindacazione. Gli attributi title, link e description corrispondono rispettivamente agli elementi RSS <title>, <link> e <description>.

Il metodo items() recupera gli oggetti da includere nel feed. Stiamo recuperando solo gli ultimi tre post pubblicati per questo feed. I metodi item_title() e item_description() ricevono ogni oggetto restituito da items() e restituiscono il titolo e la descrizione per ciascun articolo. Utilizziamo il filtro truncatewords per creare la descrizione del post con le prime 30 parole.

Ora, modifica il file blog/urls.py, importa la classe LatestPostsFeed che hai appena creato e crei un'istanza del feed in un nuovo URL:

# blog/urls.py

from .feeds import LatestPostsFeed

urlpatterns = [
   # ...
   path('feed/rss/', LatestPostsFeed(), name='post_feed'),
]

Visita http://localhost:8000/blog/feed/rss/ nel tuo browser. Dovresti vedere il feed RSS che include gli ultimi tre post:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>My cool blog</title>
<link>http://localhost:8000/blog/</link>
<description>Latest posts of my cool blog.</description>
<atom:link href="http://localhost:8000/blog/feed/rss/" rel="self"></atom:link>
<language>en-us</language>
<lastBuildDate>Mon, 23 Mar 2020 11:39:56 +0000</lastBuildDate>
<item>
<title>python is my favourite programming language</title>
<link>http://localhost:8000/blog/2020/1/11/python-my-favourite-programming-language/</link>
<description>It is a long established fact that a reader will be distracted by the readable  content of a page when looking at its layout. The point of using Lorem Ipsum …</description>
<guid>http://localhost:8000/blog/2020/1/11/python-my-favourite-programming-language/</guid></item>
<item>
<title>django is the best python web framework</title>
<link>http://localhost:8000/blog/2020/1/11/django-best-python-web-framework/</link>
<description>It is a long established fact that a reader will be distracted by the readable  content of a page when looking at its layout. The point of using Lorem Ipsum …</description>
<guid>http://localhost:8000/blog/2020/1/11/django-best-python-web-framework/</guid></item>
<item>
<title>the gods of our ancestors</title>
<link>http://localhost:8000/blog/2020/1/6/the-gods-of-our-ancestors/</link>
<description>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took …</description>
<guid>http://localhost:8000/blog/2020/1/6/the-gods-of-our-ancestors/</guid></item>
</channel>
</rss>

Se apri lo stesso URL in un client RSS, vedrai il tuo feed con un'interfaccia intuitiva.

Il passaggio finale è di aggiungere un link di iscrizione al feed nel menu laterale del blog. Apri il template blog/base.html ed aggiungi la riga seguente sotto this is my blog all'interno del div sidebar:

<!-- base.html -->

<p><a href="{% url "blog:post_feed" %}" title="Subscribe to my RSS feed">RSS feed</a></p>

Ora visita http://localhost:8000/blog/feed/rss/ nel tuo browser e dai un'occhiata al menu laterale. Il nuovo link dovrebbe portarti al feed del tuo blog:

Responsive image

per ora è tutto, rimani connesso per funzionalità più utili.


Se ti piace il mio contenuto, supportami! grazie.

Post correlati