What is the best way to embed HTML in an RSS feed?

元气小坏坏 提交于 2019-12-19 03:13:27

问题


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.


回答1:


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 =)




回答2:


Instead of writing your own RSS XML feed, consider using the Django syndication framework from django.contrib.syndication:

https://docs.djangoproject.com/en/dev/ref/contrib/syndication/




回答3:


Embedding HTML is CDATA has troubled me in the past. Hope RSS readers have evolved to handle such embeds.



来源:https://stackoverflow.com/questions/444231/what-is-the-best-way-to-embed-html-in-an-rss-feed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!