程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to implement RSS subscription for Django blog

編輯:Python

Statement : This article refers to the blog,https://www.zmrenwu.com/courses/django-blog-tutorial/materials/24/ Big guy wrote django blog The series is great , Those who are interested can learn

RSS brief introduction

RSS(Really Simple Syndication) Is a format for describing and synchronizing site content , It uses XML Format passed as content . Simply put, a website can package content to match RSS The standard XML Format document . Once the content of the site conforms to a unified specification , Then people can develop a kind of standardized reading XML Document tools to aggregate the content of major websites . For example, a reader may pay attention to a lot of blog sites , If these blog sites all support RSS If you subscribe , He just needs an aggregate reader to subscribe to these blogs , You can see all the blog updates in the aggregator tool , You don't have to visit each blog separately to see if there are any content updates .

Use Django Feed class

According to the above RSS Introduction to , We can find that the key point is to generate standardized content based on the website XML file , Fortunately, ,Django There are built-in ways to generate this document , Let's use these methods to create RSS Subscribe to documents .

First of all we have blog The root directory of the application (models.py In the directory ) Create a new one feeds.py Documents for storage and RSS Function related code . Let the queen feeds.py Write the following code in :

# blog/feeds.py
from django.contrib.syndication.views import Feed
from .models import Post
class AllPostsRssFeed(Feed):
# The title displayed on the aggregate reader
title = "Django Blog tutorial demo project "
# Jump to the address of the website through the aggregation reader
link = "/"
# Description information displayed on aggregate reader
description = "Django Blog tutorial demo project test article "
# Content items to display
def items(self):
return Post.objects.all()
# The title of the content item displayed in the aggregator
def item_title(self, item):
return '[%s] %s' % (item.category, item.title)
# Description of the content item displayed in the aggregator
def item_description(self, item):
return item.body

The code should not be difficult to understand , The main thing is to specify the XML Document content . The meaning of each property and method is annotated in the code , You just need to replace the relevant content with the description of your blog .

add to URL

Next is the designation URL Pattern , Let people visit this URL Then you can see Feed Generated content . You can put RSS Of URL The configuration is written in blog Of urls.py In the document , open blog Of urls.py file , stay urlpatterns Add inside rss The path of , Because in the AllPostsRssFeed, So we need to import it , The following code :

# blog/url.py
from blog.feeds import AllPostsRssFeed
urlpatterns = [
# Remember to introduce... At the top AllPostsRssFeed
url(r'^all/rss/$', AllPostsRssFeed(), name='rss'),
]

Modify the template

Simply modify the template , hold RSS Of URL Add to template , In the template RSS The code part of the subscription url Add in , increase {% url 'blog:rss' %}, as follows :

<a href="{% url 'blog:rss' %}" rel="external nofollow" target="_blank" title=" Subscribe to this site "><i class="rss fa fa-rss"></i></a>

Run the server , Click on the website RSS Links to subscriptions , Jump to /all/rss/, You'll see a bunch of garbled things , This is what is generated RSS Standard documents , Of course, this document is not for you to read , But to RSS The aggregate reader tool reads .

RSS Test plug-ins

You can test the subscription effect , I use Chrome browser , Installed a RSS Feed Reader Application , If you also use Chrome browser , You can add it from the store , Then you can test the subscription effect . If it's another browser , Find out about yourself RSS Just read the plug-in . My test results are as follows : You can take a look at my website RSS subscribe https://0a0z.cn/all/rss/

You can see that the subscription is successful , The information displayed in the subscription interface is that we are AllPostsRssFeed Class . Be accomplished , Now anyone can subscribe to our blog !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved