How to use CreateView with crispy-forms

808 views
Skip to first unread message

9dev...@gmail.com

unread,
May 28, 2014, 8:43:35 AM5/28/14
to django...@googlegroups.com

I want to use CreateView along with crispy-forms generated layout. However, it seems that everything I pass to self.helper in forms.py is ignored. Form renders nicely, but only with fields generated by CreateView - all fields passed to Layout are missing.

My views.py

class MyView(CreateView):
    form_class = MyForm
    model = MyModel

    def form_valid(self, form):
        pass

My forms.py:

from django.forms import ModelForm, Textarea
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout
from crispy_forms.bootstrap import FormActions

from .models import MyModel

class MyForm(ModelForm): 

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.form_method = 'POST'
        self.helper.add_input(Submit('submit', 'Submit'))
        self.helper.layout = Layout( 
            FormActions(Submit('BlahBlah', 'BlahBlah', css_class='btn-primary')))

    class Meta:
        model = MyModel
        fields = ['xxx', 'yyy']

My image_form.html:

{% load crispy_forms_tags %}
...
<form action="" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form|crispy }}
</form>

What else should I do to make it work?

Andromeda Yelton

unread,
May 28, 2014, 9:31:07 AM5/28/14
to django...@googlegroups.com
The | crispy filter provides only minimal (though stylish) rendering - if you want to render all the cool stuff in your helper you need to do {% crispy form %}.

Andromeda Yelton
LITA Board of Directors, Director-at-Large, 2013-2016
@ThatAndromeda


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2555778c-3f3f-43b4-8670-80ea8abdd1ba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages