I am currently implementing Twitter Bootstrap with Django to style forms.
Bootstrap can enhance the appearance of forms, but it requires specific CSS classes to be included.
My challenge lies in the fact that Django's generated forms using {{ form.as_p }} do not align well with Bootstrap due to the absence of these classes.
For instance, the output from Django:
<form class="horizontal-form" action="/contact/" method="post">
<div style='display:none'>
<input type='hidden' name='csrfmiddlewaretoken'
value='26c39ab41e38cf6061367750ea8c2ea8'/>
</div>
<p><label for="id_name">Name:</label> <input id="id_name" type="text" name="name" value="FOOBAR" maxlength="20" /></p>
<p><label for="id_directory">Directory:</label> <input id="id_directory" type="text" name="directory" value="FOOBAR" maxlength="60" /></p>
<p><label for="id_comment">Comment:</label> <textarea id="id_comment" rows="10" cols="40" name="comment">Lorem ipsum dolor sic amet.</textarea></p>
<p>
<label for="id_server">Server:</label>
<select name="server" id="id_server">
<option value="">---------</option>
<option value="1"
selected="selected">sydeqexcd01.au.db.com</option>
<option value="2">server1</option>
<option value="3">server2</option>
<option value="4">server3</option>
</select>
</p>
<input type="submit" value="Submit" />
</form>
Bootstrap mandates the use of
<fieldset class="control-group">
, <label class="control-label">
, and wrapping <input>
elements in a <div>
:
<fieldset class="control-group">
<label class="control-label" for="input01">Text input</label>
<div class="controls">
<input type="text" class="xlarge" name="input01">
<p class="help-text">Help text here. Be sure to fill this out like so, or else!</p>
</div>
</fieldset>
Customizing CSS labels for each form field in Django can be tedious:
Add class to Django label_tag() output
Is there a more efficient way to utilize {{ form.as_p }} or iterate through fields without manual intervention or extensive tinkering?
Regards, Victor