Django iterate over ClearableFileInput widget field

拟墨画扇 提交于 2020-01-14 14:44:16

问题


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 %}
    <div class="form-group">
    {{field.errors}}
    <label for="{{field.auto_id}}">{{field.label}}</label>
    {{field}}
 {% endfor %}

However, when the template renders the ClearableFileInput widget, I want to add some space between the href and the checkbox for clearing the widget. Any ideas on how to access those specific "parts" of the field?


回答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.



来源:https://stackoverflow.com/questions/25191940/django-iterate-over-clearablefileinput-widget-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!