Reputation: 2206
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?
Upvotes: 5
Views: 5470
Reputation: 4604
FormHelper
never had an append method AFAIK. What has indeed an append
button is the layout:
self.helper.layout.append(HTML('<p>whatever<p>'))
http://django-crispy-forms.readthedocs.org/en/latest/dynamic_layouts.html#manipulating-a-layout
For this to work you need to have a layout set:
self.helper = FormHelper()
self.helper.layout = Layout('field_1', 'field_2')
Or have a default layout set for you http://django-crispy-forms.readthedocs.org/en/latest/dynamic_layouts.html#formhelper-with-a-form-attached
http://django-crispy-forms.readthedocs.org/en/latest/dynamic_layouts.html#manipulating-a-layout
I had a typo in my other StackOverflow example, that misled you, sorry.
Upvotes: 2
Reputation: 2574
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'))
Upvotes: 2