简体   繁体   中英

django syndication feed framework: adding CSS to RSS

Adding CSS to RSS is quite simple as shown in here: http://www.petefreitag.com/item/208.cfm

Creating RSS is simple with django too as documented in here: https://docs.djangoproject.com/en/1.3/ref/contrib/syndication/

How to combine these two? Or better, how to add CSS to RSS while using django provided feed framework?

I know that i could create my own RSS xml file but why I should do that if framework works just fine? I just need a way to link css, that I could use code highlight or something like that.

What are the best practices to provide pretty RSS in django?

It is probably not worth the effort, considering this answer , but I found a way to do this, which is pretty ugly, but works regarding the XML output. The solution above didn't work in Django 1.5.

In my Feed class I overwrite the call method like so:

from django.contrib.syndication.views import Feed

class MyFeed(Feed)

    def __call__(self, request, *args, **kwargs):
        response = super(MyFeed, self).__call__(request, *args, **kwargs)

        # Add stylesheet, is it worth it?
        css = '<?xml-stylesheet type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" ?>\n'
        start = '<rss xmlns:atom'
        response.content = response.content.replace(start, css + start)

        return response

I believe the one way to do this would be to create a custom page view and point the URL to this page view (modifying the example from the Django syndication page):

(r'^beats/(?P<beat_id>\d+)/rss/$','yourapp_views_custompageview'),

Then in this page view, return <?xml-stylesheet type="text/css" href="http://you.com/rss.css"?> + BeatFeed() , where BeatFeed is your FeedClass.

I'm not using the feed framework right now, so it is difficult for me to test. Please let me know if this works or if you run into difficulties.

You might also want to look at: https://code.djangoproject.com/browser/django/trunk/django/utils/feedgenerator.py .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM