根据django的docs:
我们不需要让Django解压缩表单的字段;如果愿意,我们可以手动完成(例如,允许我们重新排序字段)。每个字段都可以使用{{form.name_of_field}}作为表单的一个属性,并且在Django模板中,将被适当地呈现。
很棒!我想让它变得更复杂……我想修改{{ form."somefield" }}
的效果这样我就可以输入:
{% for i in '012345'|make_list %}
{% with y=forloop.counter|stringformat:"s" %}
{% with somefield="somefield"|add:y %}
{{form.somefield}} #either this
{{form.somefield|add:y}} #this
{{form.somefield{{i}}}} #this
{{form.somefield{{forloop.counter}}}} #this
#or other variations of that
{% endwith %}
{% endwith %}
{% endfor %}
基本上我需要添加一些变量到"somefield"属性,以便能够引用末尾有数字的字段。
——编辑——
这是python
中表示的完整表模式from django import forms
class NewGameForm(forms.Form):
game_name = forms.CharField(max_length=50, required=True, label="", widget=forms.TextInput(attrs={'placeholder': 'Game Name', 'class': 'has-validation form-control'}))
category1 = forms.CharField(max_length=25, required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Category 1', 'class': 'form-control mt-1'}))
category2 = forms.CharField(max_length=25, required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Category 2', 'class': 'form-control mt-1'}))
category3 = forms.CharField(max_length=25, required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Category 3', 'class': 'form-control mt-1'}))
category4 = forms.CharField(max_length=25, required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Category 4', 'class': 'form-control mt-1'}))
category5 = forms.CharField(max_length=25, required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Category 5', 'class': 'form-control mt-1'}))
category6 = forms.CharField(max_length=25, required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Category 6', 'class': 'form-control mt-1'}))
q1values = forms.IntegerField(min_value=0, required=False, label="", widget=forms.TextInput(attrs={'placeholder': '200', 'class': 'form-control'}))
c1q1 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c1a1 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c2q1 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c2a1 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c3q1 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c3a1 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c4q1 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c4a1 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c5q1 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c5a1 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c6q1 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c6a1 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
q2values = forms.IntegerField(min_value=0, required=False, label="", widget=forms.TextInput(attrs={'placeholder': '400', 'class': 'form-control'}))
c1q2 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c1a2 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c2q2 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c2a2 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c3q2 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c3a2 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c4q2 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c4a2 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c5q2 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c5a2 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c6q2 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c6a2 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
q3values = forms.IntegerField(min_value=0, required=False, label="", widget=forms.TextInput(attrs={'placeholder': '600', 'class': 'form-control'}))
c1q3 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c1a3 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c2q3 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c2a3 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c3q3 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c3a3 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c4q3 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c4a3 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c5q3 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c5a3 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c6q3 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c6a3 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
q4values = forms.IntegerField(min_value=0, required=False, label="", widget=forms.TextInput(attrs={'placeholder': '800', 'class': 'form-control'}))
c1q4 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c1a4 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c2q4 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c2a4 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c3q4 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c3a4 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c4q4 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c4a4 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c5q4 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c5a4 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c6q4 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c6a4 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
q5values = forms.IntegerField(min_value=0, required=False, label="", widget=forms.TextInput(attrs={'placeholder': '1000', 'class': 'form-control'}))
c1q5 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c1a5 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c2q5 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c2a5 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c3q5 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c3a5 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c4q5 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c4a5 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c5q5 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c5a5 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
c6q5 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Question', 'class': 'form-control mt-1'}))
c6a5 = forms.CharField(required=False, label="", widget=forms.TextInput(attrs={'placeholder': 'Answer', 'class': 'form-control mt-1 mb-1'}))
给出一些上下文:
"category"+[1、2、3、4、5、6]显然是类别。"c1q1"是第一类问题1的缩写
给出一个表的样子:
q-values Category1 Category2 Category3 Category4 Category5 Category6
q1values c1q1 c2q1 c3q1 c4q1 c5q1 c6q1
q2values c1q2 c2q2 c3q2 c4q2 c5q2 c6q2
q3values c1q3 c2q3 c3q3 c4q3 c5q3 c6q3
q4values c1q4 c2q4 c3q4 c4q4 c5q4 c6q4
q5values c1q5 c2q5 c3q5 c4q5 c5q5 c6q5
通过创建templatetags文件创建自定义模板标记过滤器。(https://docs.djangoproject.com/en/3.2/howto/custom-template-tags/):
<appname>/templatetags/<appname>_extras.py
过滤器有两个参数,表单本身和字符串形式的字段名。现在可以访问字段字典了。但是,要注意必须返回一个绑定字段。这可以通过get_bound_field函数完成。(https://docs.djangoproject.com/en/3.2/ref/forms/api/customizing-boundfield)
from django import template
register = template.Library()
@register.filter
def get_field(form, field_name):
return form.fields[field_name].get_bound_field(form, field_name)
在你的模板中这样使用:
{% for i in '012345'|make_list %}
{% with "somefield"|add:i as field_name %}
{{ form|get_field:field_name }}
{% endwith %}
{% endfor %}
不要忘记加载你的自定义过滤器:
{% load <appname>_extras %}