【问题标题】:django feeds has wrong pub datedjango feed 有错误的发布日期
【发布时间】:2010-12-02 14:04:29
【问题描述】:

我有以下问题。

项目的django-piston api的handlers.py:

....
# "need" to set this for datetime.strftime()
locale.setlocale(locale.LC_TIME,'de_AT.UTF-8')

class ItemOverviewHandler(BaseHandler):
    ...
    @classmethod
    def date(self, item):
        # because of the setlocale() call the datestring is in german
        # that's good
        return item.somedatefield.date.strftime("%d. %B %Y")
 ...

现在看来这会影响项目的提要(使用 django.contrib.syndication 创建):

def item_pubdate(self, item):
    return item.pub_date #datetime field
# the rss look's like this
# that's not good
<pubDate>Die, 17 Aug 2010 14:00:00 +0200</pubDate>

(这是一个 rfc 符合日期,但在德语中 Die == Dienstag == Tuesday),因此它是无效的。

所以我需要活塞 API 响应为德语(完成)。但是提要的 pubDate 必须是英文的(不知道如何做到这一点)。

有什么建议吗?

【问题讨论】:

    标签: python django datetime localization django-piston


    【解决方案1】:

    这成功了。但我仍然愿意接受其他建议:)

    class ItemOverviewHandler(BaseHandler):
        ...
        @classmethod
        def date(self, item):
            locale.setlocale(locale.LC_TIME,'de_AT.UTF-8')
            date_string = item.somedatefield.date.strftime("%d. %B %Y")
            locale.setlocale(locale.LC_TIME,'')
            return date_string
    

    【讨论】:

    • 这会以某种方式影响在您的区域设置为奥地利语的瞬间同时请求您的提要(因此您的提要不符合 RFC)?
    【解决方案2】:

    您可以使用Babel 国际化模块。在 here 中查找 format_date 函数,该函数使用特定区域设置格式化日期时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      • 2011-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多