在RSS Feed中嵌入HTML的最佳方法是什么? [英] What is the best way to embed HTML in an RSS feed?

查看:205
本文介绍了在RSS Feed中嵌入HTML的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django的RSS功能来构建一个RSS提要。 RSS订阅项目的< description> 包含HTML标记。目前,我只是使用以下模板将HTML标记注入Feed:

  {{obj.post}} 

Django当然会翻译特殊字符( > & 等)到各自的HTML实体。



我知道我可以输出HTML,并将所有HTML代码包含在<![CDATA [...]]> 这个页面说任何一种方法都可以接受。如果是这样,是否有理由选择一种方法?如果我使用示例#2,Django是否有一个过滤器自动将HTML文本包装在CDATA标签中,或者我应该将我的模板更改为:

 <![CDATA [
{{obj.post | safe}}
]]>






修改



似乎Django会自动将RSS提要中的特殊字符(或任何XML)自动删除,无论是否通过 safe 过滤器(问题在此票中讨论)。然而,欢迎一般的答案。

解决方案

当我遇到Django这样的问题时,我的第一本能就是跑出去找一个普通的Python库,可以做我想要的。在这种情况下, PyRSS2Gen 可能是您的救世主。



它可能需要更多的时间(因为它将不知道Django对象是什么),但它应该是足够的,让你按照你的意愿做。



如果不是,它只是一个脚本。你可以把它分开,以允许原始的HTML,如果你愿意=)


I am using Django's RSS capabilities to build an RSS feed. The <description> of the RSS feed items contains HTML markup. Currently, I am just injecting the HTML markup into the feed using the following template:

{{ obj.post }}

Django, of course, translates special characters (<, >, &, etc.) to their respective HTML entities.

I know I could just output the HTML and wrap all the HTML code in <![CDATA[...]]> sections. This page says that either method is acceptable. If that's true, is there a good reason to pick one method over the other? And if I use example #2, is there a filter for Django to automatically wrap the HTML text in CDATA tags, or should I just change my template to:

<![CDATA[
{{ obj.post|safe }}
]]>


Edit

It seems that Django autoescapes special characters in RSS feeds (or any XML for that matter) no matter what, regardless of whether you pass it through the safe filter or not (the issue is discussed in this ticket). However, general answers are welcome.

解决方案

When I run into issues like this with Django my first instinct is to run off and find a normal Python lib that does what I want. In this case PyRSS2Gen might be your saviour.

It'll probably require a bit more fannying around (because it'll be unaware of what Django objects are) but it should be raw enough to let you do as you wish.

And if it isn't, it's just a script. You can hack it apart to allow raw HTML if you please =)

这篇关于在RSS Feed中嵌入HTML的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆