ソースを参照

Adjust form_field.html to pivot on widget type

This requires a new templatetag that annotates the field with the widget
type.  The motivation is so that normal forms (not using
form-horizontal) can render checkboxes differently - wrapping the input
in the label element.  This is advocated by bootstrap amongst others:
http://twitter.github.com/bootstrap/base-css.html#forms

At the moment, checkboxes rendered in a form with class
'form-horizontal' don't look right - they need some adjusting in the
CSS.
master
David Winterbottom 13年前
コミット
32c0eee9ed
2個のファイルの変更64行の追加13行の削除
  1. 36
    13
      oscar/templates/oscar/partials/form_field.html
  2. 28
    0
      oscar/templatetags/form_tags.py

+ 36
- 13
oscar/templates/oscar/partials/form_field.html ファイルの表示

@@ -1,19 +1,42 @@
1
+{% load form_tags %}
2
+
3
+{# Make the field widget type available to templates #}
4
+{% annotate_form_field field %}
5
+
1 6
 <div class="control-group {% for error in field.errors %}error{% endfor %}">
2 7
     {% if field.is_hidden %}
3 8
         {{ field }}
4 9
     {% else %}
5
-        <label for="{{ field.auto_id }}" class="control-label {% if field.field.required %}required{% endif %}">{{ field.label }}{% if field.field.required %} <span>*</span>{% endif %}</label>
6
-        <div class="controls">
7
-            {{ field }}
8
-            {% for error in field.errors %}
9
-                <span class="help-block">{{ error }}</span>
10
-            {% endfor %}
11
-            {% if field.help_text %}
12
-                <span class='help-block'>
13
-					{# We allow HTML within form help fields #}
14
-                    {{ field.help_text|safe }}
15
-                </span>
16
-            {% endif %}
17
-        </div>
10
+		{# Check if field is a checkbox as we mark these up differently #}
11
+		{% if field.widget_type == 'CheckboxInput' %}
12
+			<label for="{{ field.auto_id }}" class="control-label checkbox {% if field.field.required %}required{% endif %}">{{ field.label }}{% if field.field.required %} <span>*</span>{% endif %}
13
+				{{ field }}
14
+			</label>
15
+			<div class="controls">
16
+				{% for error in field.errors %}
17
+					<span class="help-block">{{ error }}</span>
18
+				{% endfor %}
19
+				{% if field.help_text %}
20
+					<span class='help-block'>
21
+						{# We allow HTML within form help fields #}
22
+						{{ field.help_text|safe }}
23
+					</span>
24
+				{% endif %}
25
+			</div>
26
+		{% else %}
27
+			<label for="{{ field.auto_id }}" class="control-label {% if field.field.required %}required{% endif %}">{{ field.label }}{% if field.field.required %} <span>*</span>{% endif %}</label>
28
+			<div class="controls">
29
+				{{ field }}
30
+				{% for error in field.errors %}
31
+					<span class="help-block">{{ error }}</span>
32
+				{% endfor %}
33
+				{% if field.help_text %}
34
+					<span class='help-block'>
35
+						{# We allow HTML within form help fields #}
36
+						{{ field.help_text|safe }}
37
+					</span>
38
+				{% endif %}
39
+			</div>
40
+		{% endif %}
18 41
     {% endif %}
19 42
 </div>

+ 28
- 0
oscar/templatetags/form_tags.py ファイルの表示

@@ -0,0 +1,28 @@
1
+from django import template
2
+
3
+register = template.Library()
4
+
5
+
6
+@register.tag
7
+def annotate_form_field(parser, token):
8
+    """
9
+    Set an attribute on a form field with the widget type
10
+
11
+    This means templates can use the widget type to render things differently
12
+    if they want to.  Django doesn't make this available by default.
13
+    """
14
+    args = token.split_contents()
15
+    if len(args) < 2:
16
+        raise template.TemplateSyntaxError(
17
+            "annotate_form_field tag requires a form field to be passed")
18
+    return FormFieldNode(args[1])
19
+
20
+
21
+class FormFieldNode(template.Node):
22
+    def __init__(self, field_str):
23
+        self.field = template.Variable(field_str)
24
+
25
+    def render(self, context):
26
+        field = self.field.resolve(context)
27
+        field.widget_type = field.field.widget.__class__.__name__
28
+        return ''

読み込み中…
キャンセル
保存