将 Crispy 表单与 ModelForm 一起使用

编程入门 行业动态 更新时间:2024-10-25 12:21:25
本文介绍了将 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 一起使用

本文发布于:2023-10-09 18:06:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476409.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   Crispy   ModelForm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!