Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use Crispy form with ModelForm

Tags:

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

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) 

it does not work because instance is only for 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') 

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?

like image 773
EsseTi Avatar asked Oct 27 '12 09:10

EsseTi


People also ask

What is crispy form tags in Django?

Django-crispy-forms is an application that helps to manage Django forms. It allows adjusting forms' properties (such as method, send button or CSS classes) on the backend without having to re-write them in the template.

What are forms in Django?

Django provides a Form class which is used to create HTML forms. It describes a form and how it works and appears. It is similar to the ModelForm class that creates a form by using the Model, but it does not require the Model.


1 Answers

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 %} 
like image 80
maraujop Avatar answered Oct 02 '22 19:10

maraujop