crspy-forms布局不适用于django内联formset

时间:2016-11-09 07:46:38

标签: django django-crispy-forms

这是我的表单和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' ))

我的观点:

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 %}

但它没有提供内联formset布局。如何在表单中实现清晰的表单布局?需要帮助。

0 个答案:

没有答案