0

Im writing a custom field/widget to display multiple input fields for related data, for example my product has 4 search fields, search1, search2, search3, etc.. , instead of having to define each field in my form, i want to have one field that will display as many input fields as i need ( all related data ) based on its length, here's what i have so far

class RelatedCategoryField(forms.MultiValueField):
    """
    Custom field to display multiple input boxes for a related object
    """

    def __init__(self, max_length, sub_max_length, label):
        # sub_max_length, is the max_length of each subfield
        self.total =  max_length/sub_max_length
        self.widget = CategoryWidget(self.total, label)
        fields = ()
        for num in range(self.total):
            fields += (forms.CharField(label="%s-%s" %(label, num),
                            max_length=sub_max_length),)
        super(RelatedCategoryField, self).__init__(fields, required=False)

    def compress(self, value_list):
        if value_list:
            return value_list
        return [[] for i in self.total]

class CategoryWidget(forms.MultiWidget):
    """
    Custom widget
    """
    def __init__(self, count, label):
        self.count = count
        self.label = label
        widgets = [forms.TextInput(attrs={}) for sub in range(self.count)]
        super(CategoryWidget, self).__init__(widgets)

    def decompress(self, value):
        if value:
            return value
        return [None for i in range(self.count)]

    def format_output(self, rendered_widgets):
        """
        Customize widget rendering
        """
        return render_to_string('fields/categoryfield.html', {'fields': rendered_widgets})

so basically i call this field like so:

category = RelatedCategoryField(max_length=200, sub_max_length50, label="search")

then based on sub_max_length the field determines how many fields it will create for this multivalue field and then the field label will be label+field# ( search_1, search_2, etc.. )

the above code is working fine, but my problem is that when displayed, the field only shows the label provided when the field was defined, and then it shows the input fields, i want to show each input field with its corresponding label, so to summarize my question, is it possible to display the label per field inside the multivalue field ?

4

2 回答 2

2

我在我的应用程序中做了类似的事情,定义format_output如下:

    def format_output(self, rendered_widgets):
        return mark_safe(u'<p class="placewidget">%s %s %s<br />%s %s %s %s %s %s</p>' % (
            _('Name:'), rendered_widgets[1],rendered_widgets[0],
            _('ZIP:'), rendered_widgets[2],
            _('City:'), rendered_widgets[3],
            _('State:'), rendered_widgets[4],
    ))

这会单独渲染每个小部件并使用其标签。希望能帮助到你

于 2012-01-05T12:21:36.780 回答
1

我不知道这是否是您要查找的内容,因为它涉及编辑模板而不是表单。

在您的模板中,您可以执行以下操作:

# In form_snippet.html:

{% for field in form %}
    <div class="fieldWrapper">
    {{ field.label_tag }}: {{ field }}
    </div>
{% endfor %}

来源:https ://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template

于 2011-12-13T05:50:30.867 回答