United States of America (us)

Forms

USA-specific Form helpers.

class localflavor.us.forms.USPSSelect(attrs=None)[source]

A Select widget that uses a list of US Postal Service codes as its choices.

Note

If you are looking for a form field that validates U.S. ZIP codes please use USZipCodeField.

class localflavor.us.forms.USPhoneNumberField(max_length=None, min_length=None, strip=True, empty_value='', *args, **kwargs)[source]

A form field that validates input as a U.S. phone number.

class localflavor.us.forms.USSocialSecurityNumberField(max_length=None, min_length=None, strip=True, empty_value='', *args, **kwargs)[source]

A United States Social Security number.

Checks the following rules to determine whether the number is valid:

  • Conforms to the XXX-XX-XXXX format.
  • No group consists entirely of zeroes.
  • The leading group is not “666” (block “666” will never be allocated).
  • The number is not in the promotional block 987-65-4320 through 987-65-4329, which are permanently invalid.
  • The number is not one known to be invalid due to otherwise widespread promotional use or distribution (e.g., the Woolworth’s number or the 1962 promotional number).

New in version 1.1.

class localflavor.us.forms.USStateField(required=True, widget=None, label=None, initial=None, help_text='', error_messages=None, show_hidden_initial=False, validators=(), localize=False, disabled=False, label_suffix=None)[source]

A form field that validates its input is a U.S. state, territory, or COFA territory. The input is validated against a dictionary which includes names and abbreviations.

It normalizes the input to the standard two-letter postal service abbreviation for the given state.

class localflavor.us.forms.USStateSelect(attrs=None)[source]

A Select widget that uses a list of U.S. states/territories as its choices.

class localflavor.us.forms.USZipCodeField(max_length=None, min_length=None, *args, **kwargs)[source]

A form field that validates input as a U.S. ZIP code.

Valid formats are XXXXX or XXXXX-XXXX.

Note

If you are looking for a form field with a list of U.S. Postal Service locations please use USPSSelect.

New in version 1.1.

Whitespace around the ZIP code is accepted and automatically trimmed.

Models

class localflavor.us.models.PhoneNumberField(*args, **kwargs)[source]

A CharField that checks that the value is a valid U.S.A.-style phone number.

(in the format XXX-XXX-XXXX).

class localflavor.us.models.USPostalCodeField(*args, **kwargs)[source]

A model field that stores the two-letter U.S. Postal Service abbreviation in the database.

Forms represent it as a USPSSelect` field.

Note

If you are looking for a model field that validates U.S. ZIP codes please use USZipCodeField.

class localflavor.us.models.USSocialSecurityNumberField(*args, **kwargs)[source]

A model field that stores the security number in the format XXX-XX-XXXX.

Forms represent it as forms.USSocialSecurityNumberField field.

New in version 1.1.

class localflavor.us.models.USStateField(*args, **kwargs)[source]

A model field that stores the two-letter U.S. state abbreviation in the database.

Forms represent it as a forms.USStateField field.

class localflavor.us.models.USZipCodeField(*args, **kwargs)[source]

A model field that stores the U.S. ZIP code in the database.

Forms represent it as a USZipCodeField field.

Note

If you are looking for a model field with a list of U.S. Postal Service locations please use USPostalCodeField.

New in version 1.1.

Data

localflavor.us.us_states.CONTIGUOUS_STATES = (('AL', 'Alabama'), ('AZ', 'Arizona'), ('AR', 'Arkansas'), ('CA', 'California'), ('CO', 'Colorado'), ('CT', 'Connecticut'), ('DE', 'Delaware'), ('DC', 'District of Columbia'), ('FL', 'Florida'), ('GA', 'Georgia'), ('ID', 'Idaho'), ('IL', 'Illinois'), ('IN', 'Indiana'), ('IA', 'Iowa'), ('KS', 'Kansas'), ('KY', 'Kentucky'), ('LA', 'Louisiana'), ('ME', 'Maine'), ('MD', 'Maryland'), ('MA', 'Massachusetts'), ('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'), ('OH', 'Ohio'), ('OK', 'Oklahoma'), ('OR', 'Oregon'), ('PA', 'Pennsylvania'), ('RI', 'Rhode Island'), ('SC', 'South Carolina'), ('SD', 'South Dakota'), ('TN', 'Tennessee'), ('TX', 'Texas'), ('UT', 'Utah'), ('VT', 'Vermont'), ('VA', 'Virginia'), ('WA', 'Washington'), ('WV', 'West Virginia'), ('WI', 'Wisconsin'), ('WY', 'Wyoming'))

The 48 contiguous states, plus the District of Columbia.

localflavor.us.us_states.NON_CONTIGUOUS_STATES = (('AK', 'Alaska'), ('HI', 'Hawaii'))

Non contiguous states (Not connected to mainland USA)

localflavor.us.us_states.US_TERRITORIES = (('AS', 'American Samoa'), ('GU', 'Guam'), ('MP', 'Northern Mariana Islands'), ('PR', 'Puerto Rico'), ('VI', 'Virgin Islands'))

Non-state territories.

localflavor.us.us_states.ARMED_FORCES_STATES = (('AA', 'Armed Forces Americas'), ('AE', 'Armed Forces Europe'), ('AP', 'Armed Forces Pacific'))

Military postal “states”. Note that ‘AE’ actually encompasses Europe, Canada, Africa and the Middle East.

localflavor.us.us_states.COFA_STATES = (('FM', 'Federated States of Micronesia'), ('MH', 'Marshall Islands'), ('PW', 'Palau'))

Non-US locations serviced by USPS (under Compact of Free Association).

localflavor.us.us_states.OBSOLETE_STATES = (('CM', 'Commonwealth of the Northern Mariana Islands'), ('CZ', 'Panama Canal Zone'), ('PI', 'Philippine Islands'), ('TT', 'Trust Territory of the Pacific Islands'))

Obsolete abbreviations (no longer US territories/USPS service, or code changed).

localflavor.us.us_states.US_STATES = CONTIGUOUS_STATES + NON_CONTIGUOUS_STATES

All US states.

This tuple is lazily generated and may not work as expected in all cases due to tuple optimizations in the Python interpreter which do not account for lazily generated tuples. For example:

US_STATES + ('XX', _('Select a State'))

should work as expected, but:

('XX', _('Select a State')) + US_STATES

may throw:

TypeError: can only concatenate tuple (not "proxy") to tuple

due to a Python optimization that causes the concatenation to occur before US_STATES has been lazily generated. To work around these issues, you can use a slice index ([:]) to force the generation of US_STATES before any other operations are processed by the Python interpreter:

('XX', _('Select a State')) + US_STATES[:]
localflavor.us.us_states.STATE_CHOICES = CONTIGUOUS_STATES + NON_CONTIGUOUS_STATES + US_TERRITORIES + ARMED_FORCES_STATES

All US states and territories plus DC and military mail.

This tuple is lazily generated and may not work as expected in all cases due to tuple optimizations in the Python interpreter which do not account for lazily generated tuples. For example:

STATE_CHOICES + ('XX', _('Select a State'))

should work as expected, but:

('XX', _('Select a State')) + STATE_CHOICES

may throw:

TypeError: can only concatenate tuple (not "proxy") to tuple

due to a Python optimization that causes the concatenation to occur before STATE_CHOICES has been lazily generated. To work around these issues, you can use a slice index ([:]) to force the generation of STATE_CHOICES before any other operations are processed by the Python interpreter:

('XX', _('Select a State')) + STATE_CHOICES[:]
localflavor.us.us_states.USPS_CHOICES = CONTIGUOUS_STATES + NON_CONTIGUOUS_STATES + US_TERRITORIES + ARMED_FORCES_STATES + COFA_STATES

All US Postal Service locations.

This tuple is lazily generated and may not work as expected in all cases due to tuple optimizations in the Python interpreter which do not account for lazily generated tuples. For example:

USPS_CHOICES + ('XX', _('Select a State'))

should work as expected, but:

('XX', _('Select a State')) + USPS_CHOICES

may throw:

TypeError: can only concatenate tuple (not "proxy") to tuple

due to a Python optimization that causes the concatenation to occur before USPS_CHOICES has been lazily generated. To work around these issues, you can use a slice index ([:]) to force the generation of USPS_CHOICES before any other operations are processed by the Python interpreter:

('XX', _('Select a State')) + USPS_CHOICES[:]
localflavor.us.us_states.STATES_NORMALIZED = {'nc': 'NC', 'minnesota': 'MN', 'louisiana': 'LA', 'washington': 'WA', 'northern mariana islands': 'MP', 'mn': 'MN', 'ak': 'AK', 'ind': 'IN', 'ky': 'KY', 'connecticut': 'CT', 'us virgin islands': 'VI', 'mont': 'MT', 'alabama': 'AL', 'federated states of micronesia': 'FM', 'ma': 'MA', 'sc': 'SC', 'conn': 'CT', 'mississippi': 'MS', 'n d': 'ND', 'ri': 'RI', 'hi': 'HI', 'ok': 'OK', 'sd': 'SD', 'south dakota': 'SD', 'calif': 'CA', 'ill': 'IL', 'del': 'DE', 'colorado': 'CO', 'nev': 'NV', 'oh': 'OH', 'new york': 'NY', 'pennsylvania': 'PA', 's dak': 'SD', 'penn': 'PA', 'n h': 'NH', 'pr': 'PR', 'texas': 'TX', 'new mexico': 'NM', 'ca': 'CA', 'iowa': 'IA', 'ks': 'KS', 'ohio': 'OH', 'me': 'ME', 'aa': 'AA', 'tex': 'TX', 'mp': 'MP', 'wy': 'WY', 'illinois': 'IL', 'tx': 'TX', 'il': 'IL', 'montana': 'MT', 'wyo': 'WY', 'massachusetts': 'MA', 'ala': 'AL', 'mass': 'MA', 'indiana': 'IN', 'west virginia': 'WV', 'co': 'CO', 'wis': 'WI', 'mo': 'MO', 'ariz': 'AZ', 'oregon': 'OR', 'az': 'AZ', 'rhode island': 'RI', 'la': 'LA', 'marinas islands of the pacific': 'MP', 'tn': 'TN', 'nebr': 'NE', 'ap': 'AP', 'american samao': 'AS', 'w va': 'WV', 'wa': 'WA', 'wash': 'WA', 'viginia': 'VA', 'n mex': 'NM', 'fla': 'FL', 'md': 'MD', 'nebraska': 'NE', 'tennessee': 'TN', 'new hampshire': 'NH', 'ae': 'AE', 'okla': 'OK', 'al': 'AL', 'ia': 'IA', 'sdak': 'SD', 'nm': 'NM', 'maryland': 'MD', 'fsm': 'FM', 'minn': 'MN', 'id': 'ID', 'kans': 'KS', 'calf': 'CA', 'ar': 'AR', 'n dak': 'ND', 'georgia': 'GA', 'republic of the marshall islands': 'MH', 'territory of hawaii': 'HI', 'ny': 'NY', 'ga': 'GA', 'as': 'AS', 'virginia': 'VA', 'nd': 'ND', 'oreg': 'OR', 'micronesia': 'FM', 'in': 'IN', 'dc': 'DC', 'marshall islands': 'MH', 'ne': 'NE', 'delaware': 'DE', 'neb': 'NE', 'de': 'DE', 'maine': 'ME', 'ore': 'OR', 'wisconsin': 'WI', 'florida': 'FL', 'arizona': 'AZ', 'kansas': 'KS', 'south carolina': 'SC', 'miss': 'MS', 'kan': 'KS', 'ut': 'UT', 'wisc': 'WI', 'north carolina': 'NC', 'wyoming': 'WY', 'palau': 'PW', 'nj': 'NJ', 'ark': 'AR', 'michigan': 'MI', 'marianas islands': 'MP', 'pa': 'PA', 'mi': 'MI', 'nevada': 'NV', 'oklahoma': 'OK', 'ct': 'CT', 'gu': 'GU', 'n m': 'NM', 'n j': 'NJ', 'wva': 'WV', 'ms': 'MS', 'kentucky': 'KY', 'tenn': 'TN', 'vermont': 'VT', 'or': 'OR', 'fl': 'FL', 'american samoa': 'AS', 'arkansas': 'AR', 'vi': 'VI', 'usvi': 'VI', 'deleware': 'DE', 'massachussetts': 'MA', 'district of columbia': 'DC', 'marianas islands of the pacific': 'MP', 'mich': 'MI', 'wv': 'WV', 'virgin islands': 'VI', 'nv': 'NV', 'north dakota': 'ND', 'mt': 'MT', 'alaska': 'AK', 'va': 'VA', 'colo': 'CO', 'missouri': 'MO', 'republic of palau': 'PW', 'guam': 'GU', 'vt': 'VT', 'puerto rico': 'PR', 'nmex': 'NM', 'idaho': 'ID', 'wi': 'WI', 'california': 'CA', 'hawaii': 'HI', 'nh': 'NH', 'new jersey': 'NJ', 'utah': 'UT', 'virgina': 'VA'}

Normalized versions of state names