用ModelForm使用Crispy窗体 [英] use Crispy form with ModelForm

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

问题描述

我已经进入脆皮表格,并且它似乎完全正是我想要的:渲染表单与引导布局。

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。

现在,我可以用 model.Form 替换 ModelForm 并使用脆性表单对于ModelForm?
我做了这个

Now, can I substitute the model.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()

在您的模板中: / p>

In your template:

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

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

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