Django iterates over field view ClearableFileInput
currently there is a model with an attribute model.FileField()
and when rendering in my django template i just loop over the fields like
{% for field in form.visible_fields %}
<div class="form-group">
{{field.errors}}
<label for="{{field.auto_id}}">{{field.label}}</label>
{{field}}
{% endfor %}
However, when the template displays the widget ClearableFileInput
, I want to add a space between href
and the checkbox to clear the widget. Any ideas on how to access these specific "parts" of the field?
source to share
You should override the default ClearableFileInput
and set those rendering attributes to your liking
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 in the initial attributes, but you should change them to reflect the desired result. It's pretty self-explanatory. Then, in your form, override the widgets to use this class using the attribute Meta/widgets
.
source to share