Question

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.

Was it helpful?

Solution 2

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>

OTHER TIPS

EDIT: Explanation - the filter takes the field object and returns name of the widget used by Django which can be used in comparison to string.

Use define template filter in templatetags.py:

from django import template
register = template.Library()


@register.filter('input_type')
def input_type(input):
return input.field.widget.__class__.__name__

And use it in template for example like this:

{% if field|input_type == 'Textarea' %}
<div class="col-xl-9">{{ field }}</div>
{% else %}
<div class="col-xl-3">{{ field }}</div>
{% endif %}

I had the same intention the only solution is to work with if condition on your views.py.
I mean first you give a name to your text area in the html code then move to views.py.
grab the name like this assume your text area name is "text" in views.py write this code text = request.GET ['text'] then create the if statement like

if text == '':
    return render(request, 'html page you want render')
else:
    return render(request, ' the exact page you want render if the textarea is filled ')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top