Python Django迭代ClearableFileInput小部件字段
当前有一个具有Python Django迭代ClearableFileInput小部件字段,python,html,django,django-templates,Python,Html,Django,Django Templates,当前有一个具有model.FileField()属性的模型,在django模板中呈现时,我只需迭代字段,例如 {% for field in form.visible_fields %} <div class="form-group"> {{field.errors}} <label for="{{field.auto_id}}">{{field.label}}</label> {{field}} {% endfor %}
model.FileField()
属性的模型,在django模板中呈现时,我只需迭代字段,例如
{% for field in form.visible_fields %}
<div class="form-group">
{{field.errors}}
<label for="{{field.auto_id}}">{{field.label}}</label>
{{field}}
{% endfor %}
{%for form.visible\u fields%}
{{field.errors}}
{{field.label}
{{field}}
{%endfor%}
但是,当模板呈现
ClearableFileInput
小部件时,我想在href
和用于清除小部件的复选框之间添加一些空间。关于如何访问字段的特定“部分”有什么想法吗?您必须覆盖默认的ClearableFileInput
,并根据自己的喜好设置这些呈现属性
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>'
类MyClearableInput(ClearableFileInput):
带首字母='%(首字母文本)s:%(首字母)s%(清除模板)s
%(输入文本)s:%(输入)s'
模板_与_clear='%(clear)s%(clear_复选框_标签)s'
url\标记\模板=“”
我已经设置了初始属性,但您必须更改它们以反映所需的输出。这是不言自明的。然后在表单中,使用Meta/widgets
属性重写小部件以使用此类