0

嗨 Stackoverflow 人,

我想用脆皮应用程序设置表单集的样式,但这会引起一些悲伤。

一个非常简单的模型应该展示四次。

class ItemPicture(models.Model):
    item = models.ForeignKey('Item')
    picture = ThumbnailerImageField(_('Image'),
            upload_to='pictures/', null=True, blank=True,)

表单类也很简单:

class ItemPictureForm(forms.ModelForm):
    class Meta:
        model = ItemPicture
        fields = ('picture',)

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.layout = Layout(
                Fieldset(_('Add pictures'),'picture', ),
                ButtonHolder(
                    Submit('save', _('Add'), css_class='btn btn-primary '),
                    Reset('reset', _('Cancel'), css_class='btn')))
        super(ItemPictureForm, self).__init__(*args, **kwargs)

在我的 views.py 中,我生成了表单集:

class ItemUploadPictures(FormView):
    ItemPictureFormSet = formset_factory(ItemPictureForm, extra=4)
    form_class = ItemPictureFormSet
    template_name = 'item_upload_pictures.html' 
    success_url = reverse_lazy('dashboard') 

我的麻烦是模板中的crispy期望{% crispy formset formset.form.helper %},但似乎传递的变量是form. {% crispy form %}有效,但不会显示辅助属性。如何将整个表单集信息传递给模板?

谢谢你的建议。

4

3 回答 3

3

你有没有尝试过

{% crispy formset form.form.helper %}
于 2012-08-23T11:29:01.983 回答
2

表格.py

class ItemPictureForm(forms.ModelForm):
    class Meta:
        model = ItemPicture
        fields = ('picture',)

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.layout = Layout(
                Fieldset(_('Add pictures'),'picture', ),
        # These need to be removed because they cant be prevented from duplicating
        #       ButtonHolder(
        #           Submit('save', _('Add'), css_class='btn btn-primary '),
        #           Reset('reset', _('Cancel'), css_class='btn')
        ))
        super(ItemPictureForm, self).__init__(*args, **kwargs)

manage_pictures.html

{% load crispy_forms_tags i18n %}
<form action="" method="post">
    {% csrf_token %}
    {% crispy formset formset.form.helper %}
    <div class="form-actions">
        <input type="submit" name="save" value="{% trans "Add" %}" class="btn btn-primary" id="submit-id-save"> 
        <input type="button" name="reset" value="{% trans "Cancel" %}" class="btn" id="button-id-cancel">
    </div>
</form>
{% crispy formset formset.form.helper %}
于 2013-07-27T03:11:43.993 回答
0

也许这个库可以满足您的需求:https ://github.com/runekaagaard/django-crispy-forms-fancy-formsets

于 2012-08-23T13:27:51.880 回答