tags:

views:

1090

answers:

2

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  A: 

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

Oli
A: 

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

Ritesh M Nayak