Using django-localflavor to validate form fields

django-localflavor is a third-party module that contains a collection of specific utils, such as form fields or model fields that are specific for each country. It's very useful to validate 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==2.0

Edit the settings.py file of your project and add localflavor to the INSTALLED_APPS setting as follows:

INSTALLED_APPS = [    # ...    'localflavor',]

We are going to add the United States's zip code field so that a valid U.S. zip code is required ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.