Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding extra info in form field and use it while rendering django template
    primarykey
    data
    text
    <p>I want to add extra infos for some fields of a django form, and use it in templates to add some extra html tags</p> <p>I've a django form like:</p> <pre><code>class MyForm(forms.Form): integer = forms.IntegerField( help_text='Please enter your favorite integer number', label='Favorite integer', widget=forms.TextInput(attrs={'class':'input-xlarge'})) decimal = forms.DecimalField( min_value=Decimal('0.0001'), decimal_places=4, help_text='Please enter your favorite decimal number', label='Favorite decimal', widget=forms.TextInput(attrs={'class':'input-xlarge'})) ==&gt; here I would like to say: decimal will have a new property </code></pre> <p>I am rendering MyForm in a template, using a loop over each field:</p> <pre><code>{% for item in form.visible_fields %} ==&gt; here I would like to test if item has that property to add some html tags {{ item }} {% endfor %} </code></pre> <p>I cannot use the widget's attrs, as I want to use the property info outside the rendered input tag.</p> <p>Should I <a href="https://docs.djangoproject.com/en/dev/ref/forms/fields/#creating-custom-fields" rel="nofollow">create a custom field</a>, write custom renderers for all fields I use, or is there a simpler solution I did miss ?</p> <hr> <p>Edit: So far my solution is : (simplified version) Main template:</p> <pre><code>&lt;form action="/somewhere/" method="post"&gt;{% csrf_token %} {% include 'form_field.html' with item=form.integer icon="icon-home" %} {% include 'form_field.html' with item=form.decimal icon="icon-list-alt" %} {% include 'form_field.html' with item=form.another icon="icon-signal" %} {% include 'form_field.html' with item=form.again icon="icon-time" %} ... </code></pre> <p>In form_field.html, I render a field using <a href="http://twitter.github.com/bootstrap/base-css.html#icons" rel="nofollow">Twitter's Bootstrap input-prepend div</a>:</p> <pre><code>&lt;div class="control-group"&gt; &lt;label class="control-label" for="{{ item.name }}"&gt;{{ item.label }}&lt;/label&gt; &lt;div class="controls"&gt; {% if icon %} &lt;div class="input-prepend"&gt; &lt;span class="add-on"&gt;&lt;i class="{{ icon }}"&gt;&lt;/i&gt;&lt;/span&gt; {% endif %} {{ item }} &lt;p class="help-block"&gt;{{ item.help_text }}&lt;/p&gt; {% if icon %} &lt;/div&gt; {% endif %} &lt;/div&gt; &lt;/div&gt; </code></pre> <p>All I want is to simplify the main template, using a loop rather enumerating all fields, which is error prone. So I want to move that 'icon-home' property from the template to the form definition, to have all attributes in the same place (specific widget attrs are already in the form). Ideally, in the main template I would have:</p> <pre><code>{% for item in form.visible_fields %} {% include 'form_field.html' %} {% endfor %} </code></pre> <p>I understand it can be viewed as a misunderstanding of Django's principles (site look should be in templates, not in forms)...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload