Checking for widget attribute in Django templates
<p>I'd need to pass some data from my manually generated form to the template, and would need to check the presence of that information there.</p>

<p>I'm generating my form as such (in the <code>__init__</code> part):</p>

<pre><code>for i in days_in_month:
    self.fields["info_%s" % i] = forms.ChoiceField(label="%s" % i.strftime("%a %d-%m-%Y"), 
    required=False, 
    choices=Information._meta.get_field('info').choices, 
    widget=forms.Select(attrs={'something': 'test'}))
</code></pre>

<p>What I would like to do, is to check in the template whether the attribute <code>something</code> is set, and if so, display its value (in the example above: <code>test</code>).</p>

<p>Any idea?</p>

<p>I tried with <code>{{ field.attribute }}</code> as per <a href="https://stackoverflow.com/questions/10838415/access-form-field-attributes-in-templated-django">Access form field attributes in templated Django</a> and the below:</p>

<pre><code>{% for field in form %}
    {{ field.label_tag }} {{ field }}
    {{ field.widget.attrs.something }}
{% endfor %}
</code></pre>

<p>... but that doesn't work (<code>invalid</code>).</p>

<p>Ultimately I want to do something similar to:</p>

<pre><code>{% if field.widget.attrs.something %}{{ field.widget.attrs.something }}{% endif %}
</code></pre>