3

我正在制作一个 Web 应用程序,它将在表格中显示学生列表,并在每个名称旁边有一个输入字段,允许用户给该学生一个“标签”。我无法弄清楚如何将这一切整合到一个表中。

这是我在 forms.py 中声明的表单:

class TagsForm(forms.Form):
    def __init__(self, *args, **kwargs):
        applicants = kwargs.pop('applicants')
        super(TagsForm,self).__init__(*args, **kwargs)
        for i, applicant in enumerate(applicants):
            self.fields['tag_%s' % i] = forms.CharField(label=applicant)

    def tagInput(self):
        for tags in self.cleaned_data.items():
            if tags.startswith('tag_'):
                yield (tags)

然后在 view.py 我创建表单并将其传递到上下文中:

tags_form = TagsForm(request.POST or None, applicants=applicantQuery)
if tags_form.is_valid():
    for(tags) in tags_form.tagInput():
        ...
...
context = Context({'tagsForm':tags_form, ... }
return render_to_response('review/assignment.html', context)

如您所见,我正在尝试为我将在表格中显示的每个申请人创建一个字符输入“标签”字段。然后在我的模板中我有这个:

<form name="applicants" method="POST">
<table class="sortable" id="appTable">
<tr> 
     <th>EID</th>
     <th>Name</th>
     <th>Reviewer Tags</th>
</tr>

{% for applicant in applicants %}
<tr>
    <td> <a href="../../{{ applicant.app_id }}/overview" target="_blank">{{ applicant.app_eid }}</a></td>   

    <td> {{ applicant.app_displayName }} </td>
    <td> {{ tagsForm.tags_???}} </td>
</tr>
{% endfor %}

</table><br />
{{ tags_form.non_field_errors }}
<input type="submit" value="Submit Tags"/>
</form>

我不确定如何遍历 tagsForm tags 字段,因为我已经在遍历申请者。我对 Django/HTML 很陌生,所以解决方案可能很简单,但是我搜索并找不到任何试图做这样的事情的人。谢谢您的帮助。

4

1 回答 1

0

我不确定你为什么在这里遇到问题。为什么你正在迭代某些东西的事实已经阻止你迭代其他东西?嵌套循环在 Django 模板中就像在任何其他编程环境中一样。

不用太深入你的代码,看起来你应该能够做到for tag in applicant.tags

于 2013-04-01T20:40:46.407 回答