crspy-forms layout not work for django inline form

2019-04-02 16:28发布

This is my forms and inlineformset

class EventForm(ModelForm):  

     def __init__(self, *args, **kwargs):
        super(EventForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.layout = Layout(
            Field('name'),
            Field('description'),
            Field('tags'),
        )
        self.helper.layout.append(Submit('save', 'Save'))

    class Meta:
        model = Event
        fields = ('name','description','tags', )

class GalleryForm(ModelForm): 

     def __init__(self, *args, **kwargs):
        super(GalleryForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.form_class = 'form-inline'
        self.helper.form.method = 'post'
        self.helper.form.action = ''
        self.helper.layout = Layout(
            Div(
                Div('title', css_class='col-md-4', ),
                Div('image', css_class='col-md-4', ),
                css_class='row',
            ),
            FormActions(
                Submit('submit', 'Submit'),
            ),
        )

    class Meta:
        model= Gallery
        fields = ('title', 'event',  'image')

GalleryFormSet = inlineformset_factory(Event, Gallery, extra=0, min_num=1, fields=('title', 'image' ))

My views:

class EventCreateView(FormsetMixin, CreateView):
    template_name = 'member/event_and_gallery_form.html'
    model = Event
    form_class = EventForm
    formset_class = GalleryFormSet

class EventUpdateView(FormsetMixin, UpdateView):
    template_name = 'member/event_and_gallery_form.html'
    is_update_view = True
    model = Event
    form_class = EventForm
    formset_class = GalleryFormSet

my form.html:

{% block body %}


                    <form action="." method="post" enctype="multipart/form-data">
                        {{ formset.management_form }}
                        {% csrf_token %}

                        <legend>Event</legend>
                        <div class="event">
                        {{ form|crispy}}
                        </div>

                        <legend>
                            <div class="pull-right"><a href="#" class="btn btn-inverse add-photo"><i class="icon-plus icon-white"></i> Add Photo</a></div>
                            Photo Gallery
                        </legend>
                        <div class="gallery form-inline">

                            {% for form in formset %}

                             {{form|crispy}}
                            {% endfor %}
                        </div>
                      <div class="form-actions">
                         <button type="submit" class="btn btn-primary">Save</button>
                       </div>
                     </form>                      

{% endblock %}

But it didn't give inline formset layout. How do I implement crispy form layout in my form? Help is needed.

0条回答
登录 后发表回答