【发布时间】:2020-10-08 01:24:10
【问题描述】:
我正在开发一个网络应用程序。 webapp 有一个视图,其中 for 循环运行并在每次迭代时更新三个列表。列表的长度存储在字典中。我想在每次迭代后在模板上显示列表的长度。我该怎么做呢?到目前为止,这是我的代码
views.py
def update_lst(request):
if request.method == 'POST':
lst1, lst2, lst3 = [], [], []
for i in range(10):
lst1.append(i+1)
lst2.append(i+2)
lst3.append(i+3)
context = {'length1':len(lst1), 'length2':len(lst2), 'length3':len(lst3)}
render(request, 'details.html', context)
return redirect('index.html')
return render(request, 'index.html')
details.html
<h1>Extracting details</h1>
<p>{{length1}}: length of list1</p>
<p>{{length2}}: length of list2</p>
<p>{{length3}}: length of list3</p>
现在 for 循环中的渲染函数没有执行。我做错了什么?
【问题讨论】:
标签: python django django-views render