Skip to content Skip to sidebar Skip to footer

Django Iterate Over ClearableFileInput Widget Field

currently have a model that has a model.FileField() attribute, and when rendering in my django template I just iterate over the fields, such as {% for field in form.visible_fields

Solution 1:

You have to override the default ClearableFileInput and set those rendering attributes to your taste

class MyClearableInput(ClearableFileInput):
    template_with_initial = '%(initial_text)s: %(initial)s %(clear_template)s<br />%(input_text)s: %(input)s'
    template_with_clear = '%(clear)s <label for="%(clear_checkbox_id)s">%(clear_checkbox_label)s</label>'
    url_markup_template = '<a href="{0}">{1}</a>'

I've put the initial attributes, but you have to change them to reflect your desired output. It's pretty self-explanatory. Then in your form, override the widgets to use this class using the Meta/widgets attribute.


Post a Comment for "Django Iterate Over ClearableFileInput Widget Field"