我有以下表格:
<form action="" method="post">
{% csrf_token %}
<select name="selectTeamOne">
{% for x in currentTeams %}
{% if x.teamid != 66 %}
<option value={{x.teamid}}>{{x.teamname}}</option>
{% endif %}
{% endfor %}
</select>
<select name="selectTeamTwo">
{% for x in currentTeams %}
<option value={{x.teamid}}>{{x.teamname}}</option>
{% endfor %}
</select>
<input type="submit" value="Submit" />
</form>
这是由以下视图驱动的:
def selectteams(request, soccerseason, fixturematchday):
if request.method == 'POST':
if form.is_valid():
return HttpResponse("Two different teams were selected.")
else:
return HttpResponse("Two different teams were not selected.")
fixtures = StraightredFixture.objects.filter(soccerseason=soccerseason,fixturematchday=fixturematchday).order_by('fixturedate')
currentTeams = StraightredTeam.objects.filter(currentteam=1).order_by('teamname')
cantSelectTeams = UserSelection.objects.filter(campaignno=389100069).order_by('campaignno')
return render(request, 'straightred/test.html',
{'fixtures' : fixtures,
'currentTeams' : currentTeams,
'cantSelectTeams' : cantSelectTeams,
'soccerseason' : soccerseason,
'fixturematchday' : fixturematchday})
我只是想知道最好的方法来检查用户是否已经从下拉列表中选择了同一个团队两次,并返回相关的HttpResponse,如您所见。你知道吗
任何能给我指出正确方向的建议都将不胜感激。非常感谢,艾伦。你知道吗
您可以对django窗体强制执行服务器级验证: https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other
来自django 1.8的文档:
相关问题 更多 >
编程相关推荐