George Cooper-Pearson
George Cooper-Pearson

Reputation: 173

Accessing the Input Type from forms in Django

I am trying to access the Input Type to use in HTML but it appears to coming through as blank.

According to: Django: How to access form field input_type in a template

I should be using:

{{field.field.widget.input_type}}

But it seems to be returning blank.

CODE:

{% for field in form %}
<div class="form-group">
  <label class="control-label" for="id_{{ field.name }}">{{ field.label }}</label>
      <input type="{{field.field.widget.input_type}}" class="form-control"
        name="{{ field.name }}"
        id="id_{{ field.name }}"
        value="{{ field.value }}" >
</div>
{% endfor %}

Thanks very much in advance.

Upvotes: 0

Views: 839

Answers (1)

jinchuika
jinchuika

Reputation: 333

I think an easier way to do that is by adding the HTML class to the field properties. You can do this by.

field = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))

In that whay, you would only need to do:

<label class="control-label" for="id_{{ field.name }}">{{ field.label }}</label>
{{ field }}

edit: models -> forms

Upvotes: 1

Related Questions