我是Django的新手,我正在尝试使用简洁的表单来样式化表单。我的应用程序中有一个恰好是模型表单的表单,我已经按照这里所说的https://stackoverflow.com/a/13201588/1076075使ModelForm与crisply_forms一起使用,但出现此错误:'FormHelper' object has no attribute 'append'这是我的代码的样子forms.py:from django import formsfrom models import Ticket, Ticketuserfrom crispy_forms.helper import FormHelperfrom crispy_forms.layout import Submit, Layout, Fieldset, ButtonHolderfrom crispy_forms.bootstrap import FormActionsclass 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如何克服这个问题并按照我想要的方式设置表单样式?
2 回答
绝地无双
TA贡献1946条经验 获得超4个赞
显然,表单帮助程序api已更改,您需要使用add_input而不是append现在使用:
这是直接来自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'))
添加回答
举报
0/150
提交
取消