あなたのdjangoサイトにRSSフィードを加えてください
5129 ワード
既存のプロジェクトでテストする場合は
インデックス
インデックス
1Intorduction
2Environment Setup
3Project Setup
4Add RSS Feed
導入
RSS stands for Really Simple Syndication it allows users and applications to access updates to your website. or simply it informs users or visitors about updates in your website.
環境設定
- create your virtual environment or use an existing one:
python -m venv env
- activate your virtual environment
source env/bin/activate
- install Django
pip install django
プロジェクト設定
- create new folder
mkdir django-add-rssfeed && cd django-add-rssfeed
- create new project
django-admin startproject config .
- create new app
python manage.py startapp blog
and add it to INSTALLED_APPS
- edit
blog/models.py
and paste these code in it:
from django.db import models
from django.urls import reverse
class Post(models.Model):
STATUS_CHOICES = [('published', 'Published'), ('draft', 'Draft')]
title = models.CharField(max_length=250)
content = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=12, choices=STATUS_CHOICES, default='draft')
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'slug': self.slug})
@receiver(post_save, sender=Post)
def save_slug(sender, instance=None, created=False, **kwargs):
if created:
instance.slug = slugify(instance.title)
instance.save()
- run
python manage.py makemigrations
then python manage.py migrate
and don't forget to create a superuser python manage.py createsuperuser
- add Post to
admin.py
and add some data for testing later.
RSSフィードを追加
- Create a new file
blog/feeds.py
and put this code in it.
from django.contrib.syndication.views import Feed
from django.utils.feedgenerator import Atom1Feed #optional
from .models import Post
class PostFeed(Feed):
feed_type = Atom1Feed #optional
title = "My Blog"
link = ""
description = "New Posts of My Blog"
def items(self):
return Post.objects.filter(status='published')
def item_title(self, item):
return item.title
def item_description(self, item):
return truncatewords(item.content, 30) # item.content
items()
returns a list of objects that should be included in the feed as <item>
elements. and so item_title
and item_description
are used to return a single object which will be included in <title>
and <description>
elements.
if you don't have get_absolute_url
in your model you must override item_link(self, item)
which returns reversed URL.
feed_type
is an optional attribute, it is used to change Feed Type, By default, feeds produced by Django use RSS 2.0.
- and now map it to a
blog/urls.py
:
from .feeds import PostFeed
urlpatterns = [
.......
path("feed/rss", PostFeed(), name="post-feed"),
......
]
here is how it looks like:
ソースコードはGithubにアップロードされます.https://github.com/abdulshak1999/Python/tree/main/django/website_rssfeed
詳細については、https://docs.djangoproject.com/en/3.1/ref/contrib/syndication/
Reference
この問題について(あなたのdjangoサイトにRSSフィードを加えてください), 我々は、より多くの情報をここで見つけました
https://dev.to/abdulshakoor/add-rss-feed-to-your-django-site-2k8n
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
python -m venv env
source env/bin/activate
pip install django
mkdir django-add-rssfeed && cd django-add-rssfeed
django-admin startproject config .
python manage.py startapp blog
and add it to INSTALLED_APPS
blog/models.py
and paste these code in it:
from django.db import models
from django.urls import reverse
class Post(models.Model):
STATUS_CHOICES = [('published', 'Published'), ('draft', 'Draft')]
title = models.CharField(max_length=250)
content = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=12, choices=STATUS_CHOICES, default='draft')
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'slug': self.slug})
@receiver(post_save, sender=Post)
def save_slug(sender, instance=None, created=False, **kwargs):
if created:
instance.slug = slugify(instance.title)
instance.save()
python manage.py makemigrations
then python manage.py migrate
and don't forget to create a superuser python manage.py createsuperuser
admin.py
and add some data for testing later.blog/feeds.py
and put this code in it.
from django.contrib.syndication.views import Feed
from django.utils.feedgenerator import Atom1Feed #optional
from .models import Post
class PostFeed(Feed):
feed_type = Atom1Feed #optional
title = "My Blog"
link = ""
description = "New Posts of My Blog"
def items(self):
return Post.objects.filter(status='published')
def item_title(self, item):
return item.title
def item_description(self, item):
return truncatewords(item.content, 30) # item.content
items()
returns a list of objects that should be included in the feed as <item>
elements. and so item_title
and item_description
are used to return a single object which will be included in <title>
and <description>
elements.if you don't have
get_absolute_url
in your model you must override item_link(self, item)
which returns reversed URL.feed_type
is an optional attribute, it is used to change Feed Type, By default, feeds produced by Django use RSS 2.0.blog/urls.py
:
from .feeds import PostFeed
urlpatterns = [
.......
path("feed/rss", PostFeed(), name="post-feed"),
......
]
Reference
この問題について(あなたのdjangoサイトにRSSフィードを加えてください), 我々は、より多くの情報をここで見つけました https://dev.to/abdulshakoor/add-rss-feed-to-your-django-site-2k8nテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol