【问题标题】:Use field label as placeholder with django-widget-tweaks通过 django-widget-tweaks 使用字段标签作为占位符
【发布时间】:2017-06-25 23:00:30
【问题描述】:

我正在使用 django-widget-tweaks,但无法弄清楚如何将字段变量添加为占位符,如下所示:

<div class="col-sm-10">
 {{ field|append_attr:"class:form-control"|append_attr:"placeholder:field.label" }}
 {% if field.help_text %}
   <p class="help-block"><small>{{ field.help_text }}</small></p>
 {% endif %}
</div>

上面的field.label 不计算并将字符串"field.label" 作为页面上的占位符。

一些 SO 帖子建议注册一个自定义标签/过滤器,这对于这么简单的事情来说似乎很复杂。

【问题讨论】:

    标签: django django-templates django-widget-tweaks


    【解决方案1】:

    我现在使用render_field 来呈现字段,而不是使用模板过滤器,它似乎可以工作。

    <div class="col-sm-10">
      {% render_field field class="form-control" placeholder=field.label %}
      {% if field.help_text %}
         <p class="help-block"><small>{{ field.help_text }}</small></p>
      {% endif %}
    </div>
    

    似乎表单变量不能在模板过滤器中使用,只能与 render_field 一起使用(尽管 django-widget-tweaks documentation 没有明确说明)。

    【讨论】:

      猜你喜欢
      • 2018-07-30
      • 2012-11-09
      • 1970-01-01
      • 2018-12-09
      • 2019-04-01
      • 2017-09-28
      • 2019-08-28
      • 2016-09-19
      • 1970-01-01
      相关资源
      最近更新 更多