2014-09-22 63 views
0

我有點卡住了。有人可以告訴我爲什麼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']}>

+0

如果沒有重定向從else子句在視圖而去,形式本身會告訴你它的錯誤。 – 2014-09-22 17:30:54

+0

當它沒有else子句時,沒有顯示錯誤,頁面只是刷新! – Matchday 2014-09-22 17:31:45

+0

只需在return語句之前的'else'子句中打印form.errors'。你會知道錯誤是什麼。或者,現在只是註釋掉其他整個塊 - 你會看到確切的錯誤是什麼 – karthikr 2014-09-22 17:52:40

回答

1

構建表單時,您應該通過*args**kwargs

def __init__(self, *args, **kwargs): 
    super(FantasySeasonForm,self).__init__(*args, **kwargs) 
    ... 

通過錯過了*args,當前的代碼有,如果你做了fantasyTeamForm = FantasySeasonForm(data=None)同樣的效果。即使沒有錯誤,is_valid()也會返回False,因爲表單沒有綁定到任何數據。

順便說一句,你可以遍歷玩家區域,以避免重複:

for player_id in xrange(1, 16) 
    field_name = "player%d" % player_id 
    self.fields[field_name].queryset = Player.objects.filter(team__competition__pk=2) 
+0

感謝您的提示,但仍然沒有任何變化。就好像它根本不試圖驗證表單,不管我給玩傢什麼樣的價值 – Matchday 2014-09-22 19:20:30

+0

如果你已經做了我提出的改變,它現在還在工作,請打印'form.is_bound','form。錯誤「和」else「塊中的」form.data「,並用結果更新您的問題。 – Alasdair 2014-09-22 19:22:55

+0

我已經這樣做了.. – Matchday 2014-09-22 19:36:52