将 Crispy 表单与 ModelForm 一起使用 [英] use Crispy form with ModelForm

查看:20
本文介绍了将 Crispy 表单与 ModelForm 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了脆皮表格,它似乎完全符合我的要求:使用引导程序布局渲染表单.

I've been running into crispy form, and it seems to do exactly what I want: render forms with bootstrap layout.

现在,该示例讨论如何使用 forms.Form.没关系,我可以通过编写这样的代码来创建我的:

Now, the example talk about using forms.Form. This is ok, I can create mine by writing the code like this:

class TemplateCreateForm(forms.Form):
    title = forms.CharField(label=(u'Task name'))
    description = forms.CharField(label=(u'Task description'))
    url_start = forms.CharField(label=(u'Start page url'))
    url_end = forms.CharField(label=(u'Final page url'))

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_method = 'post'
        self.helper.add_input(Submit('submit', 'Submit'))
        super(TemplateCreateForm, self).__init__(*args, **kwargs)

但是,如何进行更新?因为如果我把它放在视图中:

But, how to do the update? because if I put this in the view:

    form = TemplateCreateForm(request.POST or None, instance=template)

它不起作用,因为实例仅用于 ModelForm.

现在,我可以用 ModelForm 替换 forms.Form 并为 ModelForm 使用松脆的表单吗?我这样做了

Now, can I substitute the forms.Form with ModelForm and use crispy form for ModelForm? I did this

class TemplateCreateForm(ModelForm):
    title = forms.CharField(label=(u'Task name'))
    description = forms.CharField(label=(u'Task description'))
    url_start = forms.CharField(label=(u'Start page url'))
    url_end = forms.CharField(label=(u'Final page url'))

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_method = 'post'
        self.helper.add_input(Submit('submit', 'Submit'))
        super(TemplateCreateForm, self).__init__(*args, **kwargs)

    class Meta:
        model = Template
        exclude = ('user')

这里我添加了 Meta 类.现在:它有效,但像这样使用它是否正确?更新也以这种方式工作.

Here I added the Meta class. Now: it works, but is it correct to use it like this? The update works as well in this way.

使用表单进行更新的正确方法是什么?

What's the correct way to use forms for doing the update?

推荐答案

我是 django-crispy-forms 的首席开发人员.我不确定我是否遵循您的问题,因为它的格式有点糟糕.你到底想做什么?

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 与 ModelForms 一起工作,与处理简单表单的方式相同.它位于 Django 之上,因此不会与它混淆.它只控制你的表单渲染,但不会改变验证的工作方式、如何创建表单实例等等.

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.

我正在添加一个关于如何使用脆皮形式制作 ModelForm 的示例.

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

我相信您的第一个问题是您正在继承 forms.Form 而不是 forms.ModelForm.这就是为什么我说您的问题与 Django 相关,而不是与脆皮形式相关.

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.

稍后在您看来:

form = ExampleModelForm()

在您的模板中:

{% load crispy_forms_tags %}
{% crispy form %}

这篇关于将 Crispy 表单与 ModelForm 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆