use Crispy form with ModelForm

[亡魂溺海] 提交于 2019-11-28 18:22:06
maraujop

I'm the lead developer of django-crispy-forms. I'm not sure I follow your question as it's a bit poorly formatted. What exactly are you trying to do?

django-crispy-forms does work with ModelForms, the same way as with simple forms. It sits on top of Django, so it doesn't mess with it. It only controls your form rendering, but doesn't change how validation works, how to create form instances and so on.

EDIT:

I'm adding an example on how to do a ModelForm with crispy-forms.

class ExampleModelForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ExampleModelForm, self).__init__(*args, **kwargs)

        # If you pass FormHelper constructor a form instance
        # It builds a default layout with all its fields
        self.helper = FormHelper(self)

        # You can dynamically adjust your layout
        self.helper.layout.append(Submit('save', 'save'))

    class Meta:
        model = ExampleModel

I believe your first problem is that you were subclassing forms.Form instead of forms.ModelForm. That's why I said that your problem was Django related, not crispy-forms related.

Later in your view:

form = ExampleModelForm()

In your template:

{% load crispy_forms_tags %}
{% crispy form %}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!