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?
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'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With