Django 表单检查
Django form checking
我有以下表格:
<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})
我只是想知道检查用户是否从下拉列表中选择同一个团队两次的最佳方法和 return 如上所示的相关 HttpResponse。
任何能为我指明正确方向的建议都将不胜感激。非常感谢,艾伦。
您可以对 Django 表单强制执行服务器级别验证:
https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other
来自 django 1.8 的文档:
from django import forms
class ContactForm(forms.Form):
# Everything as before.
...
def clean(self):
cleaned_data = super(ContactForm, self).clean()
cc_myself = cleaned_data.get("cc_myself")
subject = cleaned_data.get("subject")
if cc_myself and subject:
# Only do something if both fields are valid so far.
if "help" not in subject:
raise forms.ValidationError(
"Did not send for 'help' in the subject despite "
"CC'ing yourself."
)
我有以下表格:
<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})
我只是想知道检查用户是否从下拉列表中选择同一个团队两次的最佳方法和 return 如上所示的相关 HttpResponse。
任何能为我指明正确方向的建议都将不胜感激。非常感谢,艾伦。
您可以对 Django 表单强制执行服务器级别验证: https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other
来自 django 1.8 的文档:
from django import forms
class ContactForm(forms.Form):
# Everything as before.
...
def clean(self):
cleaned_data = super(ContactForm, self).clean()
cc_myself = cleaned_data.get("cc_myself")
subject = cleaned_data.get("subject")
if cc_myself and subject:
# Only do something if both fields are valid so far.
if "help" not in subject:
raise forms.ValidationError(
"Did not send for 'help' in the subject despite "
"CC'ing yourself."
)