0

我试图在(分页)页面上有两个表单。
如果我在相关表单上点击“保存”,它们会显示得很好,我可以在管理员中看到这种变化。但是,对于 event_form,什么也没有发生,经过几天的尝试并没有找到答案,我将其发布在这里:
为什么我的 EventRecordForm 没有被提交?

这是我的表单类:

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 的表单字段,但这也不起作用。
我真的被难住了,我敢肯定这只是我错过的一件小事,但是在连续几天盯着这段代码之后,我再也看不到任何东西了。任何帮助是极大的赞赏!

4

1 回答 1

2

您可能有一个验证错误,但您永远不会看到它,因为relevance_form如果表单无效,您会在不传递 POST 数据的情况下重新实例化。不要那样做,只要让你已经拥有的表单进入渲染调用。

于 2013-05-10T11:40:40.907 回答