Python Django以文本形式在HTML网页上呈现模板标记
我的django HTML标记在通过“for”循环运行时不起作用。如果我打印出变量,它会打印出简单的列表。我甚至尝试了分配变量的不同方法。发生了什么事 view.pyPython Django以文本形式在HTML网页上呈现模板标记,python,django,templates,for-loop,Python,Django,Templates,For Loop,我的django HTML标记在通过“for”循环运行时不起作用。如果我打印出变量,它会打印出简单的列表。我甚至尝试了分配变量的不同方法。发生了什么事 view.py class CheckbackView(View): def post(self, request): statusly = request.POST.get("ConfigSubmit", "") list_init_config = statusly.splitlines()
class CheckbackView(View):
def post(self, request):
statusly = request.POST.get("ConfigSubmit", "")
list_init_config = statusly.splitlines()
with open('/data/test.txt', 'w') as fout:
fout.write(statusly)
fout.close()
converted = Con1812_to_891(statusly)
converted_config = converted.config
removed_list = converted.remove_list
return render(request, 'thunderdome/compare_edit.html', context_instance = RequestContext(request, {'config': statusly, 'converted': converted.config, 'removed': removed_list, 'objection': converted}))
渲染如下:
HTML:
{ % for line in removed %}
<li>{{ line|safe }}</li>
{ % endfor %}
{ % for line in objection.remove_list %}
<li>{{ line }}</li>
{ % endfor %}
{{objection.remove_list}}
{{removed}}
{%用于已删除%]中的行
{{line | safe}}
{%endfor%}
{objective.remove_list%}
{{line}}
{%endfor%}
{{反对.删除}
{{删除}}
模板中的语法有点错误;删除{
和%
之间的空格:
{% for line in removed %}
<li>{{ line|safe }}</li>
{% endfor %}
{% for line in objection.remove_list %}
<li>{{ line }}</li>
{% endfor %}
{%用于已删除%]中的行
{{line | safe}}
{%endfor%}
{objective.remove_list%}
{{line}}
{%endfor%}
我应该知道Django会对模板标记非常挑剔。在这个范围内,python或任何编程语言也很挑剔,因为你不能用循环代替for。