milandjukic88
milandjukic88

Reputation: 1125

How to check django ModelForm field type in html?

I need to do some logic in HTML page depending on field type. For example something to do if input is type="text" or if it is textarea or checkbox....

{% for field in formFields %}
    {{field}}
{% endfor %}

When i do something like this: {{field.field}} i get something like this:

<django.forms.fields.IntegerField object at 0x04812750>

How to use this or is better way?

Upvotes: 0

Views: 65

Answers (1)

iMom0
iMom0

Reputation: 12931

Specify field type in your views(or forms.py), do not contain complex logic in your template for field rendering,

class FormClass(ModelForm):
    my_field = forms.CharField(widget=forms.Textarea)

Then my_field will be rendered as textarea in template, each field type has its own widget, read the docs here.

Upvotes: 3

Related Questions