Wednesday, May 28, 2014

How to use CreateView with crispy-forms

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?

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

No comments:

Post a Comment