Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'FormHelper' object has no attribute 'append' while using crispy_forms in django

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

This is how my code looks in 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?

like image 927
Bharadwaj Srigiriraju Avatar asked Oct 04 '22 03:10

Bharadwaj Srigiriraju


1 Answers

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

Here's the example straight from 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'))
like image 69
tuxcanfly Avatar answered Oct 11 '22 21:10

tuxcanfly