gpt4 book ai didi

django - 如何将图像添加到 Django 的聚合 RSS 源

转载 作者:行者123 更新时间:2023-12-02 19:32:49 24 4
gpt4 key购买 nike

拥有此模型:

class MyModel(models.Model):

image = StdImageField(
upload_to="img/images",
blank=True,
variations={
"large": (1024, 1024),
"thumbnail": (150, 150, False),
"medium": (600, 600),
},
delete_orphans=True,
)

和这个 View


from django.contrib.syndication.views import Feed

class LatestItems(Feed):
title = "LatestItems"
description = "Latest Items"
link = "/sitenews/"

def items(self):
return MyModel.objects.all()

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

def item_description(self, item):
return item.description

def item_link(self, item):
return reverse('item_detail', args=[item.pk])

如何将图像添加到 RSS 文章正文?

最佳答案

创建自定义 feed_type 并在 Feed View 中设置,

from django.contrib.syndication.views import Feed
<b>from django.utils.feedgenerator import Rss201rev2Feed
from django.conf import settings


class CustomFeed(Rss201rev2Feed):
def add_item_elements(self, handler, item):
super().add_item_elements(handler, item)
handler.addQuickElement("image", item["image"])</b>


class LatestItems(Feed):
<b>feed_type = CustomFeed</b>
title = "LatestItems"
description = "Latest Items"
link = "/sitenews/"

def items(self):
return MyModel.objects.all()

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

def item_description(self, item):
return item.description

def item_link(self, item):
return reverse('item_detail', args=[item.pk])

<b>def get_context_data(self, **kwargs):
setattr(self, 'request', kwargs['request']) # to access the request object later
return super().get_context_data(**kwargs)

def item_extra_kwargs(self, item):
img_url = item.image.url
request_url = self.request.build_absolute_uri('/')[:-1]
image_url_abs = f"{request_url}{settings.STATIC_URL}{img_url}"
return {
'image': image_url_abs
}</b>

结果

Result Screenshot

关于django - 如何将图像添加到 Django 的聚合 RSS 源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61544508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com