如何美化rss喂django?

时间:2018-06-07 15:09:26

标签: python xml django

我使用Django为我的网站生成rss feed。我使用下面的代码生成Feed。

class getTenderfeeds(Feed):
     title = "Tender Links"
     link = "/rss-tender/"
     description = "Tender link feeds"

     def items(self):
         return CareerTenderCT.objects.order_by('-cart_created_datetime')[:3]

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

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

     def item_link(self, item):
         return '/view/12/'+ item.cart_absolute_url

之后在url.py中调用此类。

url(r'^rss-tender/', getTenderfeeds()),

当我打开此网址时,UI部分看起来就像下面的图像。

enter image description here

但我想改变xml中的美化,如下图所示。

enter image description here

2 个答案:

答案 0 :(得分:0)

我在模板目录中创建了一个html文件,并将所有提要呈现给该html,如下面的代码。

class getTenderfeeds(Feed):
    title = "Tender Links"
    link = "/rss-tender/"
    description = "Tender link feeds"

    def __call__(self, request, *args, **kwargs):
       try:
          obj = self.get_object(request, *args, **kwargs)
       except ObjectDoesNotExist:
          raise Http404('Feed object does not exist.')
       feedgen = self.get_feed(obj, request)
       response = HttpResponse(content_type=feedgen.content_type)
       if hasattr(self, 'item_pubdate') or hasattr(self, 'item_updateddate'):
          response['Last-Modified'] = http_date(
              timegm(feedgen.latest_post_date().utctimetuple()))
       feedgen.write(response, 'utf-8')
       response = re.sub(r'><', '>\n<', str(response))
       response = response.replace('Content-Type: application/rss+xml; charset=utf-8','')
       return render_to_response('html/feed/rss-tender.html',{ 'response':response,})

   def items(self):
       return CareerTenderCT.objects.order_by('-cart_created_datetime')[:3]

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

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

   def item_link(self, item):
       return '/view/12/'+ item.cart_absolute_url

答案 1 :(得分:0)

这很容易:

class CorrectMimeTypeFeed(DefaultFeed):
    content_type = 'application/xml; charset=utf-8'

class LatestEntriesFeed(Feed):
    feed_type = CorrectMimeTypeFeed

Django rssfeed类的默认类型为application/rss+xml; charset=utf-8

更改它,它将起作用。