我有點卡住了。有人可以告訴我爲什麼is_valid()
函數返回false並且在屏幕上也沒有顯示錯誤?我儘可能簡化了我的視圖,無論我使用何種數據填充表單,HttpResponseRedirect
總是重定向到/information
。爲什麼我的Django form is_valid()返回false?
forms.py
class FantasySeasonForm(ModelForm):
class Meta:
model = FantasyTeam
def __init__(self,*args,**kwargs):
super(FantasySeasonForm,self).__init__(**kwargs)
#filter all players to only premier league players
self.fields['player1'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player2'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player3'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player4'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player5'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player6'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player7'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player8'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player9'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player10'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player11'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player12'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player13'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player14'].queryset = Player.objects.filter(team__competition__pk=2)
self.fields['player15'].queryset = Player.objects.filter(team__competition__pk=2)
views.py
@login_required
def entry(request):
fantasyTeamForm = FantasySeasonForm() #Form to store each player in the fantasy team
seasonUserTournForm = PartialSeasonEntryForm()
# If the form has been submitted...
if request.method == 'POST':
fantasyTeamForm = FantasySeasonForm(request.POST or None) # A form bound to the POST data
if fantasyTeamForm.is_valid():
return HttpResponseRedirect('/season/entrysuccess') #page on success
else:
return HttpResponseRedirect('/information') #page on error
#OTHERWISE A GET REQUEST
args = {}
args.update(csrf(request))
args['form'] = fantasyTeamForm
args['form2'] = seasonUserTournForm
return render_to_response('entry.html', args, context_instance=RequestContext(request))
entry.html
<h2><b>Choose your team:</b></h2><br>
{% for field in form %}
{{field.error}}
{% endfor %}
{% for field in form2 %}
{{field.error}}
{% endfor %}
<form action="/season/entry/" method="post"> {% csrf_token %}
{{form2}}
<br><br>
{{form.as_ul}}
<br>
<input type="submit" value="Submit Team" />
</form>
更新
爲球隊名稱和球員選擇1-7值後,留下其餘的空白我已經印在了else語句下面的值..
form.is_bound
回報True
form.errors
回報
<ul class="errorlist"><li>player9<ul class="errorlist"><li>This field is required.</li></ul></li><li>player4<ul class="errorlist"><li>This field is required.</li></ul></li><li>player8<ul class="errorlist"><li>This field is required.</li></ul></li><li>player11<ul class="errorlist"><li>This field is required.</li></ul></li><li>player10<ul class="errorlist"><li>This field is required.</li></ul></li><li>player5<ul class="errorlist"><li>This field is required.</li></ul></li><li>player14<ul class="errorlist"><li>This field is required.</li></ul></li><li>player6<ul class="errorlist"><li>This field is required.</li></ul></li><li>player13<ul class="errorlist"><li>This field is required.</li></ul></li><li>player3<ul class="errorlist"><li>This field is required.</li></ul></li><li>player12<ul class="errorlist"><li>This field is required.</li></ul></li><li>player15<ul class="errorlist"><li>This field is required.</li></ul></li><li>player7<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
form.data
回報
<QueryDict: {'player14': [''], 'player9': [''], 'player4': ['3897'], 'team_name': ['Just Amazing'], 'player8': [''], 'player11': [''], 'player10': [''], 'player5': ['3897'], 'player1': ['1934'], 'player12': [''], 'player6': ['13017'], 'player13': [''], 'player2': ['1934'], 'player3': ['3897'], 'csrfmiddlewaretoken': ['H8dIdnepxSgEduPCT6Yiy9iYsLmXqwJy'], 'player15': [''], 'player7': ['13017']}>
如果沒有重定向從else子句在視圖而去,形式本身會告訴你它的錯誤。 – 2014-09-22 17:30:54
當它沒有else子句時,沒有顯示錯誤,頁面只是刷新! – Matchday 2014-09-22 17:31:45
只需在return語句之前的'else'子句中打印form.errors'。你會知道錯誤是什麼。或者,現在只是註釋掉其他整個塊 - 你會看到確切的錯誤是什麼 – karthikr 2014-09-22 17:52:40