Suj*_*aik 3 html python django-templates
{'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 ', ', ']}
Run Code Online (Sandbox Code Playgroud)
我如何解析通过我的视图传递到 html 文件的这个 django 变量。我想让这些数据以表格的形式显示在 html 文件中,其中显示每个键值
return render(request, 'notebook/instant_search.html', output)
Run Code Online (Sandbox Code Playgroud)
我在我的 html 文件中尝试了这个,其中输出是我通过视图传递的变量
{% for key, value in output %}
{{ key }} <br>
{% for key2 in value %}
{{ key2 }} <br>
{% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
还有这个:
{% for k in context %}
{{ k }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
但我没有得到任何输出。屏幕上没有任何内容可显示
首先,您的render函数不接受正确的参数,这就是为什么您的 html 模板上没有出现任何内容。你输入了这个:
return render(request, 'notebook/instant_search.html', output)
Run Code Online (Sandbox Code Playgroud)
正确的:
return render(request, 'notebook/instant_search.html', 'output':output)
Run Code Online (Sandbox Code Playgroud)
以上将解决模板不显示渲染函数数据的问题。
接下来是遍历字典的代码:
下面将显示列表中的每一项
{% for k, v in output.items %}
{% for i in v %}
{{ i }}
{% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
而下面的代码将显示每个列表
{% for k, v in output.items %}
{{ v }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
参考文献: https://docs.djangoproject.com/en/2.0/intro/tutorial03/
https://docs.djangoproject.com/en/2.0/topics/http/shortcuts/#render