使用字段标签作为占位符与django-widget-tweaks [英] Use field label as placeholder with django-widget-tweaks
问题描述
我正在使用django-widget-tweaks,无法找出如何添加字段变量作为占位符,如下所示:
I am using django-widget-tweaks and unable to figure out how to add a field variable as placeholder, like the following:
<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
作为页面上的占位符。
field.label
above does not evaluate and puts the string "field.label"
as the placeholder on the page.
一些SO帖子建议注册一个自定义
Some SO posts suggest registering a custom tag/filter which seems complicated for something this simple.
推荐答案
我现在使用 render_field
来渲染字段,而不是使用模板过滤器,它似乎工作。
I am now using render_field
to render the field instead of using template filters and it seems to work.
<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 < a>不明确表示)。
It seems form variables cannot be used within template filters and can only be used with render_field
(though the django-widget-tweaks documentation doesnt say that explicitly).
这篇关于使用字段标签作为占位符与django-widget-tweaks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!