Arthur

Pemberton

Full-stack web applications developer


Generating “tabindex” in Django template

July 15, 2020Arthur Pemberton0 Comments

I had a need for sequentially increasing numbers for use in a template loop with multiple INPUT tags, and I whipped up this simple template tag to help with that.

@register.simple_tag(takes_context=True)
def tabindex(context):
	if 'tabindex' not in context: context['tabindex'] = 0
	context['tabindex'] += 1
	return context['tabindex']

As an example, it could be utilized like the following.

<input type="text" name="fullname" tabindex="{% tabindex %}" />

Each time it’s called, it will self increment.

It could be extended to provide an alternate initial value (instead of 0) with an addition of an argument, but I didn’t need that myself.


Leave a Reply