Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use field label as placeholder with 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 above does not evaluate and puts the string "field.label" as the placeholder on the page.

Some SO posts suggest registering a custom tag/filter which seems complicated for something this simple.

like image 215
Anupam Avatar asked Dec 07 '22 19:12

Anupam


1 Answers

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>

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).

like image 93
Anupam Avatar answered Mar 03 '23 06:03

Anupam