From 0499097467e159ff83e4f9d2be75f2d04abc129a Mon Sep 17 00:00:00 2001 From: Loi Tran Date: Wed, 29 Feb 2012 18:42:10 +0000 Subject: [PATCH] added ability to output individual fields --- README.rst | 2 + .../templates/bootstrapform/field.html | 37 ++++++++++++++++++ .../templates/bootstrapform/form.html | 39 +------------------ bootstrapform/templatetags/bootstrap.py | 12 ++++-- 4 files changed, 49 insertions(+), 41 deletions(-) create mode 100644 bootstrapform/templates/bootstrapform/field.html diff --git a/README.rst b/README.rst index c940387..08acdcf 100644 --- a/README.rst +++ b/README.rst @@ -32,3 +32,5 @@ Usage {% load bootstrap %} {{ form|bootstrap }} + +{{ form.|bootstrap }} - To output individual fields \ No newline at end of file diff --git a/bootstrapform/templates/bootstrapform/field.html b/bootstrapform/templates/bootstrapform/field.html new file mode 100644 index 0000000..7d9272c --- /dev/null +++ b/bootstrapform/templates/bootstrapform/field.html @@ -0,0 +1,37 @@ +{% load bootstrap %} +
+ {% if field|is_checkbox %} +
+ + + {% for error in field.errors %} + {{ error }} + {% endfor %} + + {% if field.help_text %} +

+ {{ field.help_text }} +

+ {% endif %} +
+ {% else %} +
{{ field.label }}
+ +
+ {{ field }} + + {% for error in field.errors %} + {{ error }} + {% endfor %} + + {% if field.help_text %} +

+ {{ field.help_text }} +

+ {% endif %} + +
+ {% endif %} +
diff --git a/bootstrapform/templates/bootstrapform/form.html b/bootstrapform/templates/bootstrapform/form.html index fb3d802..1691af0 100644 --- a/bootstrapform/templates/bootstrapform/form.html +++ b/bootstrapform/templates/bootstrapform/form.html @@ -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 %} -
- {% if field|is_checkbox %} -
- - - {% for error in field.errors %} - {{ error }} - {% endfor %} - - {% if field.help_text %} -

- {{ field.help_text }} -

- {% endif %} -
- {% else %} -
{{ field.label }}
- -
- {{ field }} - - {% for error in field.errors %} - {{ error }} - {% endfor %} - - {% if field.help_text %} -

- {{ field.help_text }} -

- {% endif %} - -
- {% endif %} -
+ {% include 'bootstrapform/field.html' %} {% endfor %} diff --git a/bootstrapform/templatetags/bootstrap.py b/bootstrapform/templatetags/bootstrap.py index b9ded19..163cbe0 100644 --- a/bootstrapform/templatetags/bootstrap.py +++ b/bootstrapform/templatetags/bootstrap.py @@ -5,9 +5,15 @@ from django import template 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