简体   繁体   English

使用Django Python创建动态表单并在自定义模板表中显示

[英]Creating a dynamic form and displaying in a custom template table with Django Python

I am making a web application which will display a list of students in a table, and beside each name an input field allowing the user to give a "tag" to that student. 我正在创建一个Web应用程序,它将在表格中显示学生列表,并在每个名称旁边添加一个输入字段,允许用户为该学生提供“标记”。 I am having trouble figuring out how to integrate this all within a table. 我无法弄清楚如何将这一切整合到表格中。

Here is the form i declared in forms.py: 这是我在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)

then in the view.py i create the form and pass it into the context: 然后在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)

As you can see I am trying to create a character input "tag" field for each of the applicants that I will be displaying in the table. 正如您所看到的,我正在尝试为将要在表格中显示的每个申请人创建一个字符输入“标记”字段。 Then in my template I have this: 然后在我的模板中我有这个:

<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>

I'm not sure how to iterate through the tagsForm tags field, as I'm already iterating through the applicants. 我不知道如何遍历tagsForm标签字段,因为我已经在遍历申请人。 I'm pretty new to Django/HTML so the solution could be pretty simple, however I searched and couldn't find anybody who was trying to do something like this. 我是Django / HTML的新手,所以解决方案可能非常简单,但是我搜索并找不到任何试图做这样的事情的人。 Thanks for the help. 谢谢您的帮助。

I'm not sure why you're having problems here. 我不确定你为什么在这里遇到问题。 Why should the fact that you're iterating through something already prevent you from iterating through something else? 你为什么要迭代某些事情已经阻止你迭代其他东西? Nested loops are as possible in Django templates as they are in any other programming environment. Django模板中的嵌套循环与在任何其他编程环境中一样可行。

Without going too far into your code, it looks though you should just be able to do for tag in applicant.tags . 不要过多地进入你的代码,看起来你应该能够for tag in applicant.tagsfor tag in applicant.tags

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM