Sha*_*ady 1 python django rss rss2 rss-reader
我正在尝试使用 Django rss 提要在 rss 查看器应用程序上查看提要。
我用来django.contrib.syndication.views import Feed创建 RSS 提要
但它只有 3 个字段标题、描述和链接,我使用 from 添加了自定义字段 django.utils.feedgenerator import Rss201rev2Feed
它会毫无问题地生成额外的字段
但是当我用 rss 查看器打开它时,它不会显示那些额外的字段或图像
我的问题是如何让这些额外的字段显示在应用程序中?
它不显示是因为应用程序只显示标题、描述和链接而其他字段没有被处理吗?
那么我如何在描述中嵌入图像和其他字段以使其显示(最重要的是图像显示)?
我已经多次查看文档似乎无法正确处理。
这是来自 Django 应用程序的视图代码
class CustomFeedGenerator(Rss201rev2Feed):
def add_item_elements(self, handler, item):
super(CustomFeedGenerator, self).add_item_elements(handler, item)
handler.addQuickElement(u"image", item['image'])
handler.addQuickElement(u"seller_name", item['seller_name'])
handler.addQuickElement(u"price", item['price'])
class LatestPostsFeed(Feed):
title = "www.marktplaats.nl"
link = "/feeds/"
description = "Updates on changes and additions to posts published in the starter."
feed_type = CustomFeedGenerator
def items(self):
return Check_Ads_One.objects.order_by('title')[:5]
def item_title(self, item):
return item.title
def item_description(self, item):
return item.paragraph
def item_link(self, item):
return item.prod_link
def item_extra_kwargs(self, item):
return { 'image': item.image_link,
'seller_name' : item.seller_name,
'price': item.price,}
Run Code Online (Sandbox Code Playgroud)
我正在为阅读器应用程序使用 Rss Savvy
<image></image>不是 RSS 规范中规定的 Items 的标准元素here。但它是 的有效元素channel。无论如何,您可以通过多种方式将图像添加到 Feed。喜欢:
如上所述here,您可以使用附件在提要中添加图像。Django Feed Framework 也提供了add_enclosures()我们可以使用的功能。以下是基于此的两种可能的解决方案:
我假设您在模型中有一个像这样的图像字段:
class Check_Ads_One(models.Model):
image = models.ImageField()
Run Code Online (Sandbox Code Playgroud)
然后LatestPostsFeed像这样更新类:
class LatestPostsFeed(Feed):
title = "www.marktplaats.nl"
link = "/feeds/"
description = "Updates on changes and additions to posts published in the starter."
feed_type = feedgenerator.Rss201rev2Feed
# Rest of the code
def get_object(self, request, *args, **kwargs):
self.request = request
return super(LatestPostsFeed, self).get_object(request, *args, **kwargs)
def get_image_url(self, url):
return self.request.build_absolute_uri('/media{}'.format(url))
def item_enclosures(self, item):
return [feedgenerator.Enclosure(self.get_image_url(item.image.url), str(item.image.size), 'image/{}'.format(item.image.name.split('.')[-1]))]
Run Code Online (Sandbox Code Playgroud)
如果您存储的是图像的链接而不是实际图像,则使用以下方法:
import urllib
class LatestPostsFeed(Feed):
title = "www.marktplaats.nl"
link = "/feeds/"
description = "Updates on changes and additions to posts published in the starter."
feed_type = feedgenerator.Rss201rev2Feed
# Rest of the code
def get_size_and_type_of_image(self, image_url):
# This method can be moved to models.py, and on save of `Check_Ads_One` it will calculate the size and type of image from url and store in DB. Then it will much optimized solution
data = urllib.urlopen(image_url)
return data['Content-Length'], data['Content-Type']
def item_enclosures(self, item):
content_length, content_type = self.get_size_and_type_of_image(item.image)
return [feedgenerator.Enclosure(item.image, content_length, content_type)]
Run Code Online (Sandbox Code Playgroud)
CDATA(替代解决方案)中的图像信息您可以CDATA在描述中添加标签并在那里添加图像:
def item_description(self, item):
return '<![CDATA[\n{} <img src="{}" alt="{}">\n]>'.format(item.paragraph, item.image, item.title)
Run Code Online (Sandbox Code Playgroud)
可以在此StackOverflow 答案中找到更多信息
| 归档时间: |
|
| 查看次数: |
1189 次 |
| 最近记录: |