Search code examples
djangotemplatestextareaform-fields

how can i check if a field is a textarea


I know about the form field method is_checkbox that is used to check if a field of a given django form is a checkbox or not. Is there any method to check if the field is a textarea?

i have tried doing this:

{% if field.is_textarea %}

but this does not seem to work.


Solution

  • You should do something like:

    {% if field.widget...... %}
    

    The widget is responsible for the rendering, not the field it self.

    Example to check it in the shell:

    from django import forms
    from django.forms import widgets
    
    class ContactForm(forms.Form):
        text = forms.CharField(max_length=100, widget=widgets.Textarea)
    
    f = ContactForm()
    f.fields['text'].widget
    
    <django.forms.widgets.Textarea object at 0x218ccd0>