在Django中使用crispy_forms时,“ FormHelper”对象没有属性

我是Django的新手,我正在尝试使用简洁的表单来样式化表单。我的应用程序中有一个恰好是模型表单的表单,我已经按照这里所说的https://stackoverflow.com/a/13201588/1076075使ModelForm与crisply_forms一起使用,但出现此错误:


'FormHelper' object has no attribute '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

如何克服这个问题并按照我想要的方式设置表单样式?


RISEBY
浏览 264回答 2
2回答

绝地无双

显然,表单帮助程序api已更改,您需要使用add_input而不是append现在使用:这是直接来自docs的示例:from crispy_forms.helper import FormHelperfrom crispy_forms.layout import Submitclass 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'))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python