2013-05-10 69 views
0

我想在(分頁)頁面上有兩種形式。
它們顯示得很好,如果我在relevance_form上點擊「保存」,我可以在管理中看到這個改變。 然而,對於event_form,沒有任何反應,經過幾天的努力找出答案,我沒有找到答案,我在這裏發佈:
爲什麼我的EventRecordForm不被提交?在一個頁面上的多個表單 - 一個不工作:Django

這裏是我的Form類:

class EventRecordForm(ModelForm): 
"""docstring for EventRecordForm""" 
def __init__(self, *args, **kwargs): 
     super(EventRecordForm, self).__init__(*args, **kwargs) 
     self.fields['event_date'].required = True 
     self.fields['location'].required = True 
     self.fields['anti_gov'].required = True 

class Meta: 
    model = EventRecord 
    exclude = ('article', 'coder', 'last_updated','country',) 

def __unicode__(self): 
    return u'EventRecordForm' 

class RelevanceCodingRecordForm(ModelForm): 
    """(ArticleMetaInfoRecordForm description)""" 
    class Meta: 
     model = RelevanceCodingRecord 
     exclude = ('article', 'coder', 'coding_date','last_updated', 
     'screened', 'classified',) 

    def __unicode__(self): 
     return u"RelevanceCodingRecordForm" 

筆者認爲:

def assignment(request, pk): 
    """View for each assignment""" 
    if request.user.is_authenticated(): 

     #### Get correct articles 
     assignment = get_object_or_404(Assignment, pk=pk) 
     country = assignment.country.ccode 
     start_date = assignment.start_date 
     end_date = assignment.end_date 
     articles = Article.objects.filter(country=country).filter(pub_date__range=(start_date,end_date)) 
     paginator = Paginator(articles, 1) 

     #### Pagination #### 
     page = request.GET.get('page') 
     try: 
      articles = paginator.page(page) 
     except PageNotAnInteger: 
      articles = paginator.page(1) 
     except EmptryPage: 
      articles = paginator(page(paginator.num_pages)) 

     #### Forms #### 
     if request.method == 'POST': 
      if 'event' in request.POST: 
       event_form = EventRecordForm(request.POST, prefix='event') 
       if event_form.is_valid(): 
        obj = event_form.save(commit=False) 
        obj.article = paginator.page(page).object_list[0] 
        # obj.classified = True need to figure this out 
        obj.coder = request.user.coder 
        obj.country = paginator.page(page).object_list[0].country 
        obj.save() 
        redirect_to = "?page=%s" % page 
        # return HttpResponseRedirect(redirect_to) 
        return HttpResponseRedirect(reverse ('coding:assignment', args=(pk,))) 
       relevance_form = RelevanceCodingRecordForm(prefix='relevance') 
      elif 'relevance' in request.POST: 
       relevance_form = RelevanceCodingRecordForm(request.POST, prefix='relevance') 
       if relevance_form.is_valid(): 
        obj = relevance_form.save(commit=False) 
        obj.article = paginator.page(page).object_list[0] 
        obj.screened = True 
        obj.coder = request.user.coder 
        obj.save() 
        redirect_to = "?page=%s" % page 
        #return HttpResponseRedirect(redirect_to) 
        return HttpResponseRedirect(reverse ('coding:assignment', args=(pk,))) 
       event_form = EventRecordForm(prefix='event') 
     else: 
      event_form = EventRecordForm(prefix='event') 
      relevance_form = RelevanceCodingRecordForm(prefix='relevance') 

    else: 
     print ERROR 
    return render(request, 'coding/assignment.html', 
    {'articles':articles,'assignment':assignment,'event_form':event_form,'relevance_form':relevance_form}) 

我的模板:

<div id="coding"> 

<div id="relevance"> 

{% if assignment.screening == True %} 

<form action="" method="post" accept-charset="utf-8" id="form"> 
    {% csrf_token %} 
    {{ relevance_form.as_p }} 
<p><input type="submit" name="relevance" value="Save &rarr;"></p> 
</form> 

{% endif %} 
</div> 

<div id="classifying"> 
{% if assignment.classifying == True %} 

<form action="" method="post" accept-charset="utf-8" id="form"> 
    {% csrf_token %} 
    {{ event_form.as_p}} 
<p><input type="submit" name="event" value="Save &rarr;"></p> 
</form> 

{% endif %} 
</div> 
<div id="pagination"> 
    <span class="step-links"> 
     {% if articles.has_previous %} 
     <a href="?page={{ articles.previous_page_number }}">previous</a> 
     {% endif %} 

     <span class="current"> 
      Article {{ articles.number }} of {{ articles.paginator.num_pages }}. 
     </span> 
     {% if articles.has_next %} 
     <a href="?page={{ articles.next_page_number }}">next</a> 
     {% endif %} 
    </span> 
</div> 

我也試圖顯示錶單字段event_form in two co如建議here那樣通過一對一地打電話給他們,但那也不起作用。
我真的很難過,我敢肯定,這只是我錯過的一件小事,但在盯着這段代碼幾天後,我什麼都看不到了。 任何幫助,非常感謝!

回答

2

您可能有一個驗證錯誤,但您永遠不會看到它,因爲如果表單無效,您將重新實例化relevance_form,而不傳遞POST數據。不要那樣做,只要讓你已經完成的表單進入渲染調用。

+0

真棒,謝謝! – LukasKawerau 2013-05-10 15:32:38

相關問題