我有一個Python腳本創建一個字典,並將其傳遞給一個html頁面來生成一個報告。循環在HTML中的字典
在Python:
data_query= {}
data_query["service1"] = "value1"
data_query["service2"] = "value2"
return data_query
在HTML:
% for name, count in data_query:
<tr>
<td>${name}</td>
<td>${count}</td>
</tr>
% endfor
它不工作,說,它沒有返回值不夠。
我也試過(在其他問題上意見指出,我錯誤地刪除):
% for name, count in dict.iteritems():
它不給任何錯誤,但不起作用。什麼都沒顯示
${len(dict)}
給出正確的字典長度
${len(dict.iteritems())}
不顯示任何東西,似乎對我的表格式的一個奇怪的效果。
有沒有一種方法來正確迭代HTMl中的dictionart以顯示鍵和值?
編輯:我如何將字典轉移到html頁面。
from mako.lookup import TemplateLookup
from mako.runtime import Context
from mako.exceptions import text_error_template
html_lookup = TemplateLookup(directories=[os.path.join(self.dir_name)])
html_template = html_lookup.get_template('/templates/report.html')
html_data = { 'data_queries' : data_queries }
html_ctx = Context(html_file, **html_data)
try:
html_template.render_context(html_ctx)
except:
print text_error_template().render(full=False)
html_file.close()
return
html_file.close()
首先,我會建議你使用一個不同的變量名稱而不是'dict' – karthikr
對不起,'dict'是爲了這裏的代碼的目的,爲了明確這是字典。真名是'data_query'。 – Amaranth
Python代碼和HTML如何通信?是否存在某種框架? – dpk2442