<p>我需要在一个html django模板中创建一个带有select的表单:我从一个txt文件中读取值并将其存储在dict中,当我调用render to response时,我传递这个dict,但是当我试图在模板中打印它的值时,它不会打印任何内容。
以下是我的观点:</p>
<pre><code>def home(request):
i=0
d = {}
with open("static/my_file.txt") as f:
for line in f:
key=i
val = line.rstrip()
d[int(key)] = val
i=i+1
return render_to_response('home.html', var=d)
</code></pre>
<p>这是html模板中的打印:</p>
^{pr2}$
<p>能帮我吗?在</p>
<p>试试这个代替上面的jinja文件。
如果您只需要<code>var</code>的值,并且<code>var</code>是字典,那么下面的代码将适合您。在</p>
<pre><code>{% for val in var.values() %}
{{ val }}
{% endfor %}
</code></pre>