gpt4 book ai didi

python - 我想打印我在 Django 中渲染到 HTML 页面时传递的字典的键和值

转载 作者:行者123 更新时间:2023-12-02 19:26:07 26 4
gpt4 key购买 nike

This loop format in html is only printing the key but not value,I want to print both key and value in html page.

views.py

def predict(request):
if request.method == "POST":
dict = {'Name': John, 'Age': 40}
return render(request,'standalone.html',{'content':dict})
else:
return render(request,'test_homepage.html')

satandalone.html

{% for c in content %}
{{c}}
{% endfor %}

最佳答案

您使用.items()来获取二元组的可迭代对象。

def predict(request):
if request.method == "POST":
data = {'Name': 'John', 'Age': 40}
return render(request,'standalone.html',{'content': data<b>.items()</b>})
else:
return render(request,'test_homepage.html')

在模板中,您可以使用以下方式呈现:

{% for <b>k, v</b> in content %}
{{ k }}: {{ v }}
{% endfor %}

Note: Please do not name a variable dict, it overrides the reference to the dict class. Use for example data.

关于python - 我想打印我在 Django 中渲染到 HTML 页面时传递的字典的键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62377785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com