2014-01-18 41 views
5

我正在研究YouTube數據API。我試圖在Google App Engine上使用jinja2在HTML中顯示視頻統計信息中的viewCount。Google App Engine上的Jinja2中的千分隔符錯誤

當我在我的模板像指定喜歡的常量值:

{{ '{0:,}'.format(1234567890) }} 

輸出工作好爲:

1,234,567,890 

但是,如果我指定的代碼爲:

{{ '{0:,}'.format(video_item.statistics.viewCount) }} 

它不起作用,並顯示內部服務器錯誤說:

{{ '{0:,}'.format(vivi.statistics.viewCount) }}, ValueError: Cannot specify ',' with 's'. 

我不確定這是什麼意思。

然而,

{{video_item.statistics.viewCount}} 

正常工作。有人可以幫我嗎?謝謝

回答

8

@馬蒂亞斯 - 艾森thankx你的答案。它運行良好。在Jinja2中,int(some_string)不起作用。我用:

some_string | int 

所以對於我的問題,它應該是:

{{ '{0:,}'.format(video_item.statistics.viewCount | int) }} 
3

API將viewCount作爲字符串傳遞(請參閱https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.videos.list?part=statistics&id=I90H3dN2HbI&_h=2&)。

的處理程序中:

view_count = '{0:,}'.format(int(video_item.statistics.viewCount)) 

在模板中:

{{ view_count }} 

另外:http://docs.python.org/2/library/string.html#format-specification-mini-language

+0

感謝馬蒂亞斯。我也試過,但不斷收到內部服務器錯誤。 「UndefinedError:'int'is undefined」 – Afloz

+0

對不起,我的壞。 int()只能通過過濾器(http://jinja.pocoo.org/docs/templates/#builtin-filters)才能用於模板,這對你的情況並沒有什麼幫助。看到我上面編輯的答案。 –