gpt4 book ai didi

python - 遍历 django 模板中的字典

转载 作者:太空宇宙 更新时间:2023-11-04 09:51:15 27 4
gpt4 key购买 nike

{'provide a full refund of any money paid ': ['the ', 'dith ', 'with ', 'ande ', 'cor '], 'copyright laws of the place where you ar': ['e ', 'init ', ' or ', 'ate ', 's '], 'person or entity that provided you with ': ['the ', 'of ', 'ande ', ' or ', 'project '], 'michael s. hart is the originator of the': [' the ', '\n ', 's ', 'r ', ', ']}

我如何解析这个通过我的 View 传递到 html 文件的 django 变量。我想让这些数据以表格的形式显示在 html 文件中,其中显示了每个键值

return render(request, 'notebook/instant_search.html', output)

我在我的 html 文件中尝试了这个,其中输出是我通过我的 View 传递的变量

{% for key, value in output %}
{{ key }} <br>
{% for key2 in value %}
{{ key2 }} <br>
{% endfor %}
{% endfor %}

还有这个:

{% for k in context %}
{{ k }}
{% endfor %}

但是我没有得到任何输出。屏幕上什么也没有显示

最佳答案

首先,您的 render 函数没有接受正确的参数,这就是为什么您的 html 模板上没有出现任何内容的原因。你输入了这个:

return render(request, 'notebook/instant_search.html', output)

正确的是:

return render(request, 'notebook/instant_search.html', 'output':output)

以上将解决模板不显示来自渲染函数的数据的问题。

接下来是遍历字典的代码:

下面会显示列表中的每一项

{% for k, v in output.items %}
{% for i in v %}
{{ i }}
{% endfor %}
{% endfor %}

而下面的代码将显示每个列表

{% for k, v in output.items %}
{{ v }}
{% endfor %}

引用资料: https://docs.djangoproject.com/en/2.0/intro/tutorial03/

https://docs.djangoproject.com/en/2.0/topics/http/shortcuts/#render

关于python - 遍历 django 模板中的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47742823/

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