无法从模板中的for循环获得我想要的东西?

时间:2021-07-14 17:28:10

I have template like this:

我有这样的模板:

{% for a in As%}
    <div>blah--blah--</div>
    <input type="hidden" name="doSomeThingTarget" value={{a.xx}}>
    <input type="hidden" name="submit" value="doSomeThing">
{% endfor%}

The loop may execute couple of times, and show some submit buttons.

循环可以执行几次,并显示一些提交按钮。

In python file, I code:

在python文件中,我编码:

target = self.request.get('doSomeThingTarget')

You know, I try to use value={{a.xx}} to hold the value in a particular loop, so, when I click one of the buttons, I can figure out what's target I need to process. BUT whichever I click, I just get the value of the first loop. What's wrong with my code? How can I implement my intention?

你知道,我尝试使用value = {{a.xx}}来保存特定循环中的值,因此,当我单击其中一个按钮时,我可以找出我需要处理的目标。但无论我点击哪个,我都会得到第一个循环的值。我的代码出了什么问题?我该如何实现我的意图?

Thank you.

1 个解决方案

#1


1  

You're defining a single form, with multiple copies of a hidden input element with the same name. As a result, you only get the one copy (though if you used get_all, you'd get an array of all of them).

您正在定义一个表单,其中包含具有相同名称的隐藏输入元素的多个副本。因此,您只能获得一个副本(但如果您使用了get_all,则会获得所有这些副本的数组)。

Instead, you should include the form start and end tags inside the loop, making each iteration its own form.

相反,您应该在循环中包含表单开始和结束标记,使每个迭代成为自己的形式。

#1


1  

You're defining a single form, with multiple copies of a hidden input element with the same name. As a result, you only get the one copy (though if you used get_all, you'd get an array of all of them).

您正在定义一个表单,其中包含具有相同名称的隐藏输入元素的多个副本。因此,您只能获得一个副本(但如果您使用了get_all,则会获得所有这些副本的数组)。

Instead, you should include the form start and end tags inside the loop, making each iteration its own form.

相反,您应该在循环中包含表单开始和结束标记,使每个迭代成为自己的形式。