added ability to output individual fields

This commit is contained in:
Loi Tran 2012-02-29 18:42:10 +00:00
parent f7c5a705b2
commit 0499097467
4 changed files with 49 additions and 41 deletions

View File

@ -32,3 +32,5 @@ Usage
{% load bootstrap %}
{{ form|bootstrap }}
{{ form.<field name>|bootstrap }} - To output individual fields

View File

@ -0,0 +1,37 @@
{% load bootstrap %}
<div class="control-group{% if field.errors %} error{% endif %}">
{% if field|is_checkbox %}
<div class="controls">
<label class="checkbox">
{{ field }} <span>{{ field.label }}</span>
</label>
{% for error in field.errors %}
<span class="help-inline">{{ error }}</span>
{% endfor %}
{% if field.help_text %}
<p class="help-block">
{{ field.help_text }}
</p>
{% endif %}
</div>
{% else %}
<div class="control-label">{{ field.label }}</div>
<div class="controls">
{{ field }}
{% for error in field.errors %}
<span class="help-inline">{{ error }}</span>
{% endfor %}
{% if field.help_text %}
<p class="help-block">
{{ field.help_text }}
</p>
{% endif %}
</div>
{% endif %}
</div>

View File

@ -1,5 +1,3 @@
{% load bootstrap %}
{{ form.non_field_errors }}
{% for field in form.hidden_fields %}
@ -7,41 +5,6 @@
{% endfor %}
{% for field in form.visible_fields %}
<div class="control-group{% if field.errors %} error{% endif %}">
{% if field|is_checkbox %}
<div class="controls">
<label class="checkbox">
{{ field }} <span>{{ field.label }}</span>
</label>
{% for error in field.errors %}
<span class="help-inline">{{ error }}</span>
{% endfor %}
{% if field.help_text %}
<p class="help-block">
{{ field.help_text }}
</p>
{% endif %}
</div>
{% else %}
<div class="control-label">{{ field.label }}</div>
<div class="controls">
{{ field }}
{% for error in field.errors %}
<span class="help-inline">{{ error }}</span>
{% endfor %}
{% if field.help_text %}
<p class="help-block">
{{ field.help_text }}
</p>
{% endif %}
</div>
{% endif %}
</div>
{% include 'bootstrapform/field.html' %}
{% endfor %}

View File

@ -5,9 +5,15 @@ from django import template
register = template.Library()
@register.filter
def bootstrap(form):
def bootstrap(element):
element_type = element.__class__.__name__.lower()
if element_type == 'boundfield':
template = get_template("bootstrapform/field.html")
context = Context({'field': element})
else:
template = get_template("bootstrapform/form.html")
context = Context({'form': form})
context = Context({'form': element})
return template.render(context)
@register.filter