我有以下表格:
# coding=utf-8
class SelectTwoTeams(BootstrapForm):
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
self.currentSelectedTeam1 = kwargs.pop('currentSelectedTeam1', None)
self.currentSelectedTeam2 = kwargs.pop('currentSelectedTeam2', None)
self.currentfixturematchday = kwargs.pop('currentfixturematchday', None)
self.currentCampaignNo = kwargs.pop('currentCampaignNo', None)
super(SelectTwoTeams, self).__init__(*args, **kwargs)
cantSelectTeams = UserSelection.objects.select_related().filter(~Q(fixtureid__fixturematchday=self.currentfixturematchday),campaignno=self.currentCampaignNo)
if not cantSelectTeams:
queryset = StraightredTeam.objects.filter(currentteam = 1)
else:
queryset = StraightredTeam.objects.filter(currentteam = 1).exclude(teamid__in=cantSelectTeams.values_list('teamselectionid', flat=True))
self.fields['team1'].queryset = queryset
self.fields['team2'].queryset = queryset
self.fields['team1'].initial = self.currentSelectedTeam1
self.fields['team2'].initial = self.currentSelectedTeam2
team1 = forms.ModelChoiceField(queryset=StraightredTeam.objects.none(), empty_label=None,
widget=forms.Select(attrs={"class":"select-format",'onchange': 'this.form.submit();'}))
team2 = forms.ModelChoiceField(queryset=StraightredTeam.objects.none(), empty_label=None,
widget=forms.Select(attrs={"class":"select-format",'onchange': 'this.form.submit();'}))
def clean(self):
cleaned_data = self.cleaned_data # individual field's clean methods have already been called
team1 = cleaned_data.get("team1")
team2 = cleaned_data.get("team2")
if team1 == team2:
raise forms.ValidationError("You picked the same team!")
return cleaned_data
如果我在我的HTML文件中使用以下内容并选择相同的两个团队,它会正确地说#34;你选择了同一个团队!":
<form action="" method="post">
{% csrf_token %}
{{ form }}
</form>
但是,如果我使用以下内容:
<form action="" method="post">
{% csrf_token %}
{{ form.team1 }}{{ form.team2 }}
</form>
我没有得到反馈。当我选择相同的两支球队时,什么也没发生。任何关于为什么分离字段的想法都会使它停止工作?
非常感谢,艾伦。
答案 0 :(得分:1)
这些之间的区别不是&#34;分隔字段&#34;。您已经从完整的表单表示(包括表单标签,布局,最重要的是错误)切换到仅显示两个输入字段本身。
当然,这很好,因为大多数情况下你会想要它给你的额外布局控制,但是你需要记住放入基本{{1}的所有其他东西。版本确实。
{{ form }}