Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2455 -- Added newforms USStateSelect widget in django.contrib.…

…localflavor.usa

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4545 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 907241e299a4ed9077bbeb6fbfdccce738a8e2a8 1 parent b8fa80b
Adrian Holovaty authored February 20, 2007
10  django/contrib/localflavor/usa/forms.py
@@ -3,7 +3,7 @@
3 3
 """
4 4
 
5 5
 from django.newforms import ValidationError
6  
-from django.newforms.fields import Field, RegexField, EMPTY_VALUES
  6
+from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
7 7
 from django.utils.translation import gettext
8 8
 
9 9
 class USZipCodeField(RegexField):
@@ -34,3 +34,11 @@ def clean(self, value):
34 34
             except KeyError:
35 35
                 pass
36 36
         raise ValidationError(u'Enter a U.S. state or territory.')
  37
+
  38
+class USStateSelect(Select):
  39
+    """
  40
+    A Select widget that uses a list of U.S. states/territories as its choices.
  41
+    """
  42
+    def __init__(self, attrs=None):
  43
+        from us_states import STATE_CHOICES # relative import
  44
+        super(USStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
64  django/contrib/localflavor/usa/us_states.py
... ...
@@ -1,10 +1,72 @@
1 1
 """
2  
-A mapping of state misspellings/abbreviations to normalized abbreviations.
  2
+A mapping of state misspellings/abbreviations to normalized abbreviations, and
  3
+an alphabetical list of states for use as `choices` in a formfield.
3 4
 
4 5
 This exists in this standalone file so that it's only imported into memory
5 6
 when explicitly needed.
6 7
 """
7 8
 
  9
+STATE_CHOICES = (
  10
+    ('AL', 'Alabama'),
  11
+    ('AK', 'Alaska'),
  12
+    ('AS', 'American Samoa'),
  13
+    ('AZ', 'Arizona'),
  14
+    ('AR', 'Arkansas'),
  15
+    ('CA', 'California'),
  16
+    ('CO', 'Colorado'),
  17
+    ('CT', 'Connecticut'),
  18
+    ('DE', 'Deleware'),
  19
+    ('DC', 'District of Columbia'),
  20
+    ('FM', 'Federated States of Micronesia'),
  21
+    ('FL', 'Florida'),
  22
+    ('GA', 'Georgia'),
  23
+    ('GU', 'Guam'),
  24
+    ('HI', 'Hawaii'),
  25
+    ('ID', 'Idaho'),
  26
+    ('IL', 'Illinois'),
  27
+    ('IN', 'Indiana'),
  28
+    ('IA', 'Iowa'),
  29
+    ('KS', 'Kansas'),
  30
+    ('KY', 'Kentucky'),
  31
+    ('LA', 'Louisiana'),
  32
+    ('ME', 'Maine'),
  33
+    ('MH', 'Marshall Islands'),
  34
+    ('MD', 'Maryland'),
  35
+    ('MI', 'Michigan'),
  36
+    ('MN', 'Minnesota'),
  37
+    ('MS', 'Mississippi'),
  38
+    ('MO', 'Missouri'),
  39
+    ('MT', 'Montana'),
  40
+    ('NE', 'Nebraska'),
  41
+    ('NV', 'Nevada'),
  42
+    ('NH', 'New Hampshire'),
  43
+    ('NJ', 'New Jersey'),
  44
+    ('NM', 'New Mexico'),
  45
+    ('NY', 'New York'),
  46
+    ('NC', 'North Carolina'),
  47
+    ('ND', 'North Dakota'),
  48
+    ('MP', 'Northern Mariana Islands'),
  49
+    ('OH', 'Ohio'),
  50
+    ('OK', 'Oklahoma'),
  51
+    ('OR', 'Oregon'),
  52
+    ('PW', 'Palau'),
  53
+    ('PA', 'Pennsylvania'),
  54
+    ('PR', 'Puerto Rico'),
  55
+    ('RI', 'Rhode Island'),
  56
+    ('SC', 'South Carolina'),
  57
+    ('SD', 'South Dakota'),
  58
+    ('TN', 'Tennessee'),
  59
+    ('TX', 'Texas'),
  60
+    ('UT', 'Utah'),
  61
+    ('VT', 'Vermont'),
  62
+    ('VI', 'Virgin Islands'),
  63
+    ('VA', 'Virginia'),
  64
+    ('WA', 'Washington'),
  65
+    ('WV', 'West Virginia'),
  66
+    ('WI', 'Wisconsin'),
  67
+    ('WY', 'Wyoming'),
  68
+)
  69
+
8 70
 STATES_NORMALIZED = {
9 71
     'ak': 'AK',
10 72
     'al': 'AL',
72  tests/regressiontests/forms/tests.py
@@ -2222,7 +2222,7 @@
2222 2222
 ...     special_name = CharField()
2223 2223
 ...     def clean_special_name(self):
2224 2224
 ...         raise ValidationError("Something's wrong with '%s'" % self.clean_data['special_name'])
2225  
- 
  2225
+
2226 2226
 >>> f = EscapingForm({'special_name': "Nothing to escape"}, auto_id=False)
2227 2227
 >>> print f
2228 2228
 <tr><th>Special name:</th><td><ul class="errorlist"><li>Something&#39;s wrong with &#39;Nothing to escape&#39;</li></ul><input type="text" name="special_name" value="Nothing to escape" /></td></tr>
@@ -3349,7 +3349,75 @@
3349 3349
 >>> f.clean('')
3350 3350
 u''
3351 3351
 
3352  
-# UKPostcodeField ##############################################################
  3352
+# USStateSelect ###############################################################
  3353
+
  3354
+USStateSelect is a Select widget that uses a list of U.S. states/territories
  3355
+as its choices.
  3356
+>>> from django.contrib.localflavor.usa.forms import USStateSelect
  3357
+>>> w = USStateSelect()
  3358
+>>> print w.render('state', 'IL')
  3359
+<select name="state">
  3360
+<option value="AL">Alabama</option>
  3361
+<option value="AK">Alaska</option>
  3362
+<option value="AS">American Samoa</option>
  3363
+<option value="AZ">Arizona</option>
  3364
+<option value="AR">Arkansas</option>
  3365
+<option value="CA">California</option>
  3366
+<option value="CO">Colorado</option>
  3367
+<option value="CT">Connecticut</option>
  3368
+<option value="DE">Deleware</option>
  3369
+<option value="DC">District of Columbia</option>
  3370
+<option value="FM">Federated States of Micronesia</option>
  3371
+<option value="FL">Florida</option>
  3372
+<option value="GA">Georgia</option>
  3373
+<option value="GU">Guam</option>
  3374
+<option value="HI">Hawaii</option>
  3375
+<option value="ID">Idaho</option>
  3376
+<option value="IL" selected="selected">Illinois</option>
  3377
+<option value="IN">Indiana</option>
  3378
+<option value="IA">Iowa</option>
  3379
+<option value="KS">Kansas</option>
  3380
+<option value="KY">Kentucky</option>
  3381
+<option value="LA">Louisiana</option>
  3382
+<option value="ME">Maine</option>
  3383
+<option value="MH">Marshall Islands</option>
  3384
+<option value="MD">Maryland</option>
  3385
+<option value="MI">Michigan</option>
  3386
+<option value="MN">Minnesota</option>
  3387
+<option value="MS">Mississippi</option>
  3388
+<option value="MO">Missouri</option>
  3389
+<option value="MT">Montana</option>
  3390
+<option value="NE">Nebraska</option>
  3391
+<option value="NV">Nevada</option>
  3392
+<option value="NH">New Hampshire</option>
  3393
+<option value="NJ">New Jersey</option>
  3394
+<option value="NM">New Mexico</option>
  3395
+<option value="NY">New York</option>
  3396
+<option value="NC">North Carolina</option>
  3397
+<option value="ND">North Dakota</option>
  3398
+<option value="MP">Northern Mariana Islands</option>
  3399
+<option value="OH">Ohio</option>
  3400
+<option value="OK">Oklahoma</option>
  3401
+<option value="OR">Oregon</option>
  3402
+<option value="PW">Palau</option>
  3403
+<option value="PA">Pennsylvania</option>
  3404
+<option value="PR">Puerto Rico</option>
  3405
+<option value="RI">Rhode Island</option>
  3406
+<option value="SC">South Carolina</option>
  3407
+<option value="SD">South Dakota</option>
  3408
+<option value="TN">Tennessee</option>
  3409
+<option value="TX">Texas</option>
  3410
+<option value="UT">Utah</option>
  3411
+<option value="VT">Vermont</option>
  3412
+<option value="VI">Virgin Islands</option>
  3413
+<option value="VA">Virginia</option>
  3414
+<option value="WA">Washington</option>
  3415
+<option value="WV">West Virginia</option>
  3416
+<option value="WI">Wisconsin</option>
  3417
+<option value="WY">Wyoming</option>
  3418
+</select>
  3419
+
  3420
+# UKPostcodeField #############################################################
3353 3421
 
3354 3422
 UKPostcodeField validates that the data is a valid UK postcode.
3355 3423
 >>> from django.contrib.localflavor.uk.forms import UKPostcodeField

0 notes on commit 907241e

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