Using django-localflavor to validate form fields
django-localflavor
is a third-party module that contains a collection of utilities, such as form fields or model fields, that are specific for each country. It’s very useful for validating local regions, local phone numbers, identity card numbers, social security numbers, and so on. The package is organized into a series of modules named after ISO 3166 country codes.
Install django-localflavor
using the following command:
pip install django-localflavor==3.1
Edit the settings.py
file of your project and add localflavor
to the INSTALLED_APPS
setting, as follows:
INSTALLED_APPS = [
# ...
'localflavor',
]
You are going to add the United States zip code field so that a valid United States zip code is required to create a new order.
Edit the forms.py
file of the orders
application and make it look like the following:
from django import forms
from localflavor.us.forms import USZipCodeField...