[英]Django shows names instead of objects
我有输入数组
class ChooseColumnsForm(forms.Form):
choices = (
(1, ("")),
(2, ("sn")),
(3, ("cn")),
(4, ("password")),
(5, ("uid")),
(4, ("mail"))
)
columns = []
def set_columns(self, col):
for i in range(0,col):
self.columns.append(forms.ChoiceField(self.choices))
我想给他们看
{% for choice in columns %}
<td>{{ choice }}</td>
{% endfor %}
但是我不是输入,而是输入的名称。
我想得到什么
我知道我可以像thiu一样输入:
{% for choice in form_col.columns %}
<td>{{ form_col.lol }}</td>
{% endfor %}
其中form_col.lol是forms.py中的ChoiceField,但是后来我无法获得所有选择:
print(form.cleaned_data['lol'])
该显示仅在最后一个ChoiceField中选择。
是的,我想我找到了。
首先在form.py中,我删除了变量列。 我必须向字段中添加新表单,而表单是forms.Form中的数组。 我还必须创建自己的render(?)函数as_row:
class ChooseColumnsForm(forms.Form):
choices = (
(1, ("")),
(2, ("sn")),
(3, ("cn")),
(4, ("password")),
(5, ("uid")),
(4, ("mail"))
)
def set_columns(self, col):
for i in range(0,col):
self.fields['column'+str(i)] = forms.ChoiceField(self.choices)
def as_row(self):
return self._html_output(
normal_row='<td>%(errors)s%(field)s%(help_text)s</td>',
error_row='<td colspan="2">%s</td>',
row_ender='</td>',
help_text_html='<br /><span class="helptext">%s</span>',
errors_on_separate_row=False)
在views.py中,我正在发送此
form_col = ChooseColumnsForm()
form_col.set_columns(length)
return render(request, 'django_ldap/add_students.html', {'form': form, 'form_col': form_col.as_row(), 'file2': file2})
而在html中
<thead>
{{ form_col }}
</thead>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.