這不是最有效的方法,但是如果您想将已經呈現為html的表作為變量傳遞給視圖,則可以完成任務。更好的方法是隻傳遞資料,然後讓模闆使用模闆邏輯循環并在您想要的位置輸出變量。在data = {'sentiment_analysis_result': [{'article_title': u'These Digital Locks Help Keep Tabs on Tenants', 'score': u'0.139613', 'type': u'positive'}, {'article_title': u'You Can Get a $50 Phone From Amazon, If You Don\u2019t Mind the Ads', 'score': u'0.239663', 'type': u'positive'}]}
table_string = '
for key, value in data.iteritems():
table_string += ''
table_string += '
' + key + ''
table_string += '
'
table_string += '
'
for i, d in enumerate(value):
if i == 0:
table_string += '
'
for k in d.iterkeys():
table_string += '
' + k + ''
table_string += '
'
table_string += '
'
for v in d.itervalues():
table_string += '
' + v + ''
table_string += '
'
table_string += '
'
table_string += '
'
print(table_string)