Is there a better way to code this in a template ?
(I would prefer the classnames are set in the template and not in views.py)
This is within a {% for field in form %}
{% if field.field.widget.input_type != "checkbox" %}
{% with class="flex-1 block w-full focus:ring-indigo-500 focus:border-indigo-500 min-w-0 rounded-none rounded-r-md sm:text-sm border-gray-300" %}
{% render_field field class=class %}
{% endwith %}
{% else %}
{% with class="block focus:ring-indigo-500 focus:border-indigo-500 min-w-0 rounded-none sm:text-sm" %}
{% render_field field class=class %}
{% endwith %}
{% endif %}
you could do a template tag?
{% use my fieldtag %}
{% fieldtag field=field %}
and somewhere in a templatetags
folder, fieldtag.py
:
from django import template
register = template.Library()
@register.inclusion_tag('fieldtag/index.html', name='fieldtag', takes_context=True)
def fieldtag(context, field=None):
if not field:
return {}
# tests here to determine the css classes to apply
# my god what a long classname :)
return {
'class_name': 'flex-1 block w-full focus:ring-indigo-500 focus:border-indigo-500 min-w-0 rounded-none rounded-r-md sm:text-sm border-gray-300'
}
Oh, ok I didn’t get into template tags till now. Thanks a lot.
my god what a long classname
Welcome to the world of TailWindCSS !