其他分享
首页 > 其他分享> > 在’django中使用crispy_forms时,’FormHelper’对象没有属性’append’

在’django中使用crispy_forms时,’FormHelper’对象没有属性’append’

作者:互联网

我是Django的新手,我正在尝试用酥脆的形式设计表单.我在我的应用程序中有一个表格恰好是一个模型,我已经按照https://stackoverflow.com/a/13201588/1076075所说的那样使ModelForm与crispy_forms一起工作,但是得到了这个错误:

‘FormHelper’对象没有属性’append’

这是我的代码在forms.py中的样子:

from django import forms
from models import Ticket, Ticketuser
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Fieldset, ButtonHolder
from crispy_forms.bootstrap import FormActions

class AddTicketForm(forms.ModelForm):

  def __init__(self, *args, **kwargs):
    super(AddTicketForm, self).__init__(*args, **kwargs)
    self.helper = FormHelper(self)
    self.helper.append(Submit('save', 'save'))

  class Meta:
    model = Ticket
    fields = ('id', 'comment')

  def process(self):
    data = self.cleaned_data
    data = data['comment']
    return data

如何克服这个问题并根据我的需要设计表单样式?

解决方法:

显然,表单助手api已经改变,你需要使用add_input而不是现在追加:

以下是the docs的例子:

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

class ExampleForm(forms.Form):
    [...]
    def __init__(self, *args, **kwargs):
        super(ExampleForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id = 'id-exampleForm'
        self.helper.form_class = 'blueForms'
        self.helper.form_method = 'post'
        self.helper.form_action = 'submit_survey'

        self.helper.add_input(Submit('submit', 'Submit'))

标签:python,django,django-forms,django-crispy-forms
来源: https://codeday.me/bug/20190625/1284322.html