Django遍历ClearableFileInput小部件字段 [英] Django iterate over ClearableFileInput widget field

查看:147
本文介绍了Django遍历ClearableFileInput小部件字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前有一个具有 model.FileField()属性的模型,当在django模板中渲染时,我仅遍历字段,例如

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 %}

但是,何时模板呈现了 ClearableFileInput 小部件,我想在 href 与清除小部件的复选框之间添加一些空间。关于如何访问字段的特定部分的任何想法?

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?

推荐答案

您必须覆盖默认的 ClearableFileInput 并将这些渲染属性设置为您的喜好

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>'

我已经设置了初始属性,但是您必须更改它们以反映所需的输出。这很不言自明。然后在表单中,使用 Meta / widgets 属性覆盖小部件以使用此类。

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.

这篇关于Django遍历ClearableFileInput小部件字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆