Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added PasswordInput, HiddenInput, FileInput, Select to django.newform…

…s.widgets

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3956 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c0eab791df84293ac5249784f39874ef44bbe4a6 1 parent c58fe99
Adrian Holovaty authored November 01, 2006

Showing 1 changed file with 46 additions and 3 deletions. Show diff stats Hide diff stats

  1. 49  django/newforms/widgets.py
49  django/newforms/widgets.py
@@ -2,9 +2,10 @@
2 2
 HTML Widget classes
3 3
 """
4 4
 
5  
-__all__ = ('Widget', 'TextInput', 'Textarea', 'CheckboxInput')
  5
+__all__ = ('Widget', 'TextInput', 'PasswordInput', 'HiddenInput', 'FileInput', 'Textarea', 'CheckboxInput', 'Select')
6 6
 
7 7
 from django.utils.html import escape
  8
+from itertools import chain
8 9
 
9 10
 # Converts a dictionary to a single string with key="value", XML-style.
10 11
 # Assumes keys do not need to be XML-escaped.
@@ -17,15 +18,29 @@ def __init__(self, attrs=None):
17 18
     def render(self, name, value):
18 19
         raise NotImplementedError
19 20
 
20  
-class TextInput(Widget):
  21
+class Input(Widget):
  22
+    "Base class for all <input> widgets (except type='checkbox', which is special)"
  23
+    input_type = None # Subclasses must define this.
21 24
     def render(self, name, value, attrs=None):
22 25
         if value is None: value = ''
23  
-        final_attrs = dict(self.attrs, type='text', name=name)
  26
+        final_attrs = dict(self.attrs, type=self.input_type, name=name)
24 27
         if attrs:
25 28
             final_attrs.update(attrs)
26 29
         if value != '': final_attrs['value'] = value # Only add the 'value' attribute if a value is non-empty.
27 30
         return u'<input %s />' % flatatt(final_attrs)
28 31
 
  32
+class TextInput(Input):
  33
+    input_type = 'text'
  34
+
  35
+class PasswordInput(Input):
  36
+    input_type = 'password'
  37
+
  38
+class HiddenInput(Input):
  39
+    input_type = 'hidden'
  40
+
  41
+class FileInput(Input):
  42
+    input_type = 'file'
  43
+
29 44
 class Textarea(Widget):
30 45
     def render(self, name, value, attrs=None):
31 46
         if value is None: value = ''
@@ -41,3 +56,31 @@ def render(self, name, value, attrs=None):
41 56
             final_attrs.update(attrs)
42 57
         if value: final_attrs['checked'] = 'checked'
43 58
         return u'<input %s />' % flatatt(final_attrs)
  59
+
  60
+class Select(Widget):
  61
+    def __init__(self, attrs=None, choices=()):
  62
+        # choices can be any iterable
  63
+        self.attrs = attrs or {}
  64
+        self.choices = choices
  65
+
  66
+    def render(self, name, value, attrs=None, choices=()):
  67
+        if value is None: value = ''
  68
+        final_attrs = dict(self.attrs, name=name)
  69
+        if attrs:
  70
+            final_attrs.update(attrs)
  71
+        output = [u'<select %s>' % flatatt(final_attrs)]
  72
+        str_value = str(value) # Normalize to string.
  73
+        for option_value, option_label,  in chain(self.choices, choices):
  74
+            selected_html = (str(option_value) == str_value) and ' selected="selected"' or ''
  75
+            output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(option_label)))
  76
+        output.append(u'</select>')
  77
+        return u'\n'.join(output)
  78
+
  79
+class SelectMultiple(Widget):
  80
+    pass
  81
+
  82
+class RadioSelect(Widget):
  83
+    pass
  84
+
  85
+class CheckboxSelectMultiple(Widget):
  86
+    pass

0 notes on commit c0eab79

Please sign in to comment.
Something went wrong with that request. Please try again.