使用django formset时出错

时间:2016-05-27 10:17:32

标签: python django

我需要根据需要多次克隆表单。我的目标是:用户选择他需要多少股票然后重定向到具有相同数量的表格的页面(可能我会使用像Ajax这样的东西,但现在对我来说太难了)。发现我应该使用formset。现在我有错误ManagementForm data is missing or has been tampered with

使用django-datetime-widgetdjango-crispy-forms

forms.py

from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import *
from crispy_forms.bootstrap import *
from datetimewidget.widgets import DateWidget

class InputDates(forms.Form): 
    Date_start = forms.DateField(widget=DateWidget(
        # attrs={'id':"datetimeid1"},
        usel10n = True,
        bootstrap_version=3
    ),
        label='first date',
        required=True,
    )

    Date_end = forms.DateField(widget=DateWidget(
            # attrs={'id':"datetimeid2"},
            usel10n = True,
            bootstrap_version=3
        ),
        label='last date',
        required=True,
    )

    Stock = forms.ChoiceField(
        choices=(   ('GG', 'Google'),
                    ('APPL', 'Apple'),
                    ('SR', 'Smth1'),
                    ('GZ', 'Smth2'),),
        required=True,
        label='Choose your stock'
    )

    def __init__(self, *args, **kwargs):
        super(InputDates, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Field('Stock'),
            Field('Date_start'),
            Field('Date_end'),
        )

class BTN(forms.Form):
    def __init__(self, *args, **kwargs):
        super(BTN, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            ButtonHolder(
                Submit('submit', 'Make graph', css_class='btn btn-default')
            ),
        )

views.py

from django.shortcuts import render
from .forms import *
from django.forms.formsets import formset_factory

def stocks(request):
    btn = BTN(request.POST or None)
    StocksFormset = formset_factory(InputDates, extra=2)
    if request.method == 'POST':
        formset = StocksFormset(request.POST)

        if formset.is_valid():
            return render(request, 'home.html')

    else:
        formset = StocksFormset()

    return render(request, 'stocks.html', {'formset' : formset, 'BTN' : btn})

stocks.html

{% extends "base.html" %}
{% load staticfiles %}
{% load crispy_forms_tags %}

{% block content %}

<form action="" method="post" class="text-left">
{% csrf_token %}
    {{ formset.management_form }}
    {% for form in formset %}
          {% crispy form form.helper %} 
    {% endfor %}
</form>
    {% crispy BTN BTN.helper %}

{% endblock %}

Django的{​​{1}}文档中,我发现我应该使用formset,一切都会好的,但它对我不起作用。

1 个答案:

答案 0 :(得分:0)

我认为问题在于您尝试使用crispy表单呈现formset表单的方式。

具体而言,您需要在帮助程序中将form_tag设置为False,因为目前它将为集合中的每个表单呈现单独的<form>标记,这将彻底混淆浏览器:

class InputDates(forms.Form):
    # ...

    def __init__(self, *args, **kwargs):
        super(InputDates, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = False

考虑到您手动呈现form_tag代码,您还需要在False表单助手上将BTN设置为<form>

这是relevant part of the documentation

  

在您的视图中,您需要更改布局或为每个formset的表单使用不同的帮助。确保将form_tag属性设置为False,否则您将获得3个单独的表单。