diff --git a/django/contrib/localflavor/usa/forms.py b/django/contrib/localflavor/usa/forms.py index e56087545a..4d2cb5e90a 100644 --- a/django/contrib/localflavor/usa/forms.py +++ b/django/contrib/localflavor/usa/forms.py @@ -3,7 +3,7 @@ USA-specific Form helpers """ from django.newforms import ValidationError -from django.newforms.fields import Field, RegexField, EMPTY_VALUES +from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES from django.utils.translation import gettext class USZipCodeField(RegexField): @@ -34,3 +34,11 @@ class USStateField(Field): except KeyError: pass raise ValidationError(u'Enter a U.S. state or territory.') + +class USStateSelect(Select): + """ + A Select widget that uses a list of U.S. states/territories as its choices. + """ + def __init__(self, attrs=None): + from us_states import STATE_CHOICES # relative import + super(USStateSelect, self).__init__(attrs, choices=STATE_CHOICES) diff --git a/django/contrib/localflavor/usa/us_states.py b/django/contrib/localflavor/usa/us_states.py index 46bd0019e5..5333763645 100644 --- a/django/contrib/localflavor/usa/us_states.py +++ b/django/contrib/localflavor/usa/us_states.py @@ -1,10 +1,72 @@ """ -A mapping of state misspellings/abbreviations to normalized abbreviations. +A mapping of state misspellings/abbreviations to normalized abbreviations, and +an alphabetical list of states for use as `choices` in a formfield. This exists in this standalone file so that it's only imported into memory when explicitly needed. """ +STATE_CHOICES = ( + ('AL', 'Alabama'), + ('AK', 'Alaska'), + ('AS', 'American Samoa'), + ('AZ', 'Arizona'), + ('AR', 'Arkansas'), + ('CA', 'California'), + ('CO', 'Colorado'), + ('CT', 'Connecticut'), + ('DE', 'Deleware'), + ('DC', 'District of Columbia'), + ('FM', 'Federated States of Micronesia'), + ('FL', 'Florida'), + ('GA', 'Georgia'), + ('GU', 'Guam'), + ('HI', 'Hawaii'), + ('ID', 'Idaho'), + ('IL', 'Illinois'), + ('IN', 'Indiana'), + ('IA', 'Iowa'), + ('KS', 'Kansas'), + ('KY', 'Kentucky'), + ('LA', 'Louisiana'), + ('ME', 'Maine'), + ('MH', 'Marshall Islands'), + ('MD', 'Maryland'), + ('MI', 'Michigan'), + ('MN', 'Minnesota'), + ('MS', 'Mississippi'), + ('MO', 'Missouri'), + ('MT', 'Montana'), + ('NE', 'Nebraska'), + ('NV', 'Nevada'), + ('NH', 'New Hampshire'), + ('NJ', 'New Jersey'), + ('NM', 'New Mexico'), + ('NY', 'New York'), + ('NC', 'North Carolina'), + ('ND', 'North Dakota'), + ('MP', 'Northern Mariana Islands'), + ('OH', 'Ohio'), + ('OK', 'Oklahoma'), + ('OR', 'Oregon'), + ('PW', 'Palau'), + ('PA', 'Pennsylvania'), + ('PR', 'Puerto Rico'), + ('RI', 'Rhode Island'), + ('SC', 'South Carolina'), + ('SD', 'South Dakota'), + ('TN', 'Tennessee'), + ('TX', 'Texas'), + ('UT', 'Utah'), + ('VT', 'Vermont'), + ('VI', 'Virgin Islands'), + ('VA', 'Virginia'), + ('WA', 'Washington'), + ('WV', 'West Virginia'), + ('WI', 'Wisconsin'), + ('WY', 'Wyoming'), +) + STATES_NORMALIZED = { 'ak': 'AK', 'al': 'AL', diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 34f1907c5e..c4502b2202 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -2222,7 +2222,7 @@ Validation errors are HTML-escaped when output as HTML. ... special_name = CharField() ... def clean_special_name(self): ... raise ValidationError("Something's wrong with '%s'" % self.clean_data['special_name']) - + >>> f = EscapingForm({'special_name': "Nothing to escape"}, auto_id=False) >>> print f Special name: @@ -3349,7 +3349,75 @@ u'' >>> f.clean('') u'' -# UKPostcodeField ############################################################## +# USStateSelect ############################################################### + +USStateSelect is a Select widget that uses a list of U.S. states/territories +as its choices. +>>> from django.contrib.localflavor.usa.forms import USStateSelect +>>> w = USStateSelect() +>>> print w.render('state', 'IL') + + +# UKPostcodeField ############################################################# UKPostcodeField validates that the data is a valid UK postcode. >>> from django.contrib.localflavor.uk.forms import UKPostcodeField