Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Custom form field</strong> + <strong>multiple inheritance</strong> + <strong>a class that stores the icon class name</strong></p> <p>First, let's define a class that captures the icon class name. We'll call it <em>IconName</em>:</p> <pre><code>class IconName(object): def get_icon_name(self): return self._icon_name def set_icon_name(self, value): self._icon_name = value icon_name = property(get_icon_name, set_icon_name) </code></pre> <p>You need to come up with custom field type for each form field type you use. Here is how it's done with IntegerField. Your custom field class needs to inherit from the original django form field class (IntegerField, in this case) and the <em>IconName</em> class.</p> <pre><code>class CustomCharField(CharField, IconName): pass </code></pre> <p>After you have defined all of the custom form field types that your form requires, update your form with them. Next, you need to initialize the <code>icon_name</code> for each form field in the form's <code>__init__()</code>:</p> <pre><code>def __init__(self, *args, **kwargs): super(forms.Form, self).__init__(*args, **kwargs) self.fields['integer'].icon_name = 'icon-home' ... </code></pre> <p>In your template, you can do:</p> <pre><code>{% for item in form.visible_fields %} &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 item.field.icon_name %} &lt;div class="input-prepend"&gt; &lt;span class="add-on"&gt;&lt;i class="{{ item.field.icon_name }}"&gt;&lt;/i&gt;&lt;/span&gt; {% endif %} {{ item }} &lt;p class="help-block"&gt;{{ item.help_text }}&lt;/p&gt; {% if item.field.icon_name %} &lt;/div&gt; {% endif %} &lt;/div&gt; &lt;/div&gt; {% endfor %} </code></pre> <p>I have not tried this myself but it should work (assuming I understand you correctly)</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