本文介绍了在django中使用crispy_forms时,'FormHelper'对象没有属性'append'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的新手,我正在尝试以脆弱的形式来形式化。我的应用程序中有一个表单恰好是一个模型,我已经遵循了这里所说的内容。使ModelForm与crispy_forms一起工作,但是收到此错误:

I'm new to Django, and I am trying to style forms with crispy forms. I have a form in my app which happens to be a modelform, and I've followed what has been said here https://stackoverflow.com/a/13201588/1076075 to make ModelForm work with crispy_forms, but getting this error:

'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

如何解决这个问题,并按照我想要的方式设计样式?

How to get over this and style the form like how I want?

推荐答案

显然,帮助器api的形式发生了变化,您需要使用 add_input 而不是 append 现在:

Apparently, the form helper api has changed, you need to use add_input instead of append now:

这是从:

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'))

这篇关于在django中使用crispy_forms时,'FormHelper'对象没有属性'append'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:23