Skip to content

Commit

Permalink
Merge pull request tzangms#9 from loitran/master
Browse files Browse the repository at this point in the history
Single form field outputting
  • Loading branch information
tzangms committed Mar 1, 2012
2 parents f7c5a70 + 0499097 commit 2d7da2a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 41 deletions.
2 changes: 2 additions & 0 deletions README.rst
Expand Up @@ -32,3 +32,5 @@ Usage
{% load bootstrap %}

{{ form|bootstrap }}

{{ form.<field name>|bootstrap }} - To output individual fields
37 changes: 37 additions & 0 deletions bootstrapform/templates/bootstrapform/field.html
@@ -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>
39 changes: 1 addition & 38 deletions bootstrapform/templates/bootstrapform/form.html
@@ -1,47 +1,10 @@
{% load bootstrap %}

{{ form.non_field_errors }}

{% for field in form.hidden_fields %}
{{ field }}
{% 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 %}

12 changes: 9 additions & 3 deletions bootstrapform/templatetags/bootstrap.py
Expand Up @@ -5,9 +5,15 @@
register = template.Library()

@register.filter
def bootstrap(form):
template = get_template("bootstrapform/form.html")
context = Context({'form': 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': element})

return template.render(context)

@register.filter
Expand Down

0 comments on commit 2d7da2a

Please sign in to comment.