Top Stories

Ankaj Gupta
December 18, 2022

ModuleNotFoundError: No module named 'phonenumbers'

Installing Django Phone Number Field

This article provides a comprehensive guide for installing and setting up django-phonenumber-field in your Django project. This library provides a Django model field and form fields for storing and validating phone numbers using the phonenumbers library.

Prerequisites

Before installing django-phonenumber-field, ensure you have the following installed:

  • Python 3.6 or higher
  • Django 2.2 or higher
  • pip package manager

Installation Steps

Step 1: Install django-phonenumber-field

Install the main package using pip:

pip install django-phonenumber-field

Step 2: Install django-phonenumbers

Install the phonenumbers package which provides phone number parsing and validation:

pip install django-phonenumbers

Step 3: Add to INSTALLED_APPS

Add phonenumber_field to your INSTALLED_APPS in your Django settings file:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'phonenumber_field',  # Add this line
    'myapp',
]

Step 4: Configure Settings (Optional)

Configure the default region for phone number parsing in your settings:

# In settings.py
PHONENUMBER_DEFAULT_REGION = 'US'  # Default to US region

Note: The default region is used when parsing phone numbers that don't include a country code. Change this to match your target audience's primary region.

Usage Example

Here's how to use PhoneNumberField in your Django models:

from django.db import models
from phonenumber_field.modelfields import PhoneNumberField

class UserProfile(models.Model):
    name = models.CharField(max_length=100)
    phone_number = PhoneNumberField()  # International format
    local_phone = PhoneNumberField(region='US')  # US format
    
    def __str__(self):
        return self.name

Using in Forms

The library also provides form fields for easy integration:

from django import forms
from phonenumber_field.formfields import PhoneNumberField

class UserProfileForm(forms.Form):
    name = forms.CharField(max_length=100)
    phone_number = PhoneNumberField()
    
    def clean_phone_number(self):
        phone_number = self.cleaned_data['phone_number']
        # Automatic validation happens here
        return phone_number

Key Features

International Format

Stores phone numbers in E.164 international format

Automatic Validation

Validates phone numbers during form submission

Region Support

Supports different regions and formats

Database Efficient

Uses CharField with max_length=128 in database

Common Installation Commands

Quick Install: Use these commands if you need to install both packages together:

pip install django-phonenumber-field django-phonenumbers

Summary

django-phonenumber-field provides a robust solution for handling phone numbers in Django applications. It offers international format support, automatic validation, and seamless integration with Django models and forms.

After installation, simply add phonenumber_field to your INSTALLED_APPS and start using PhoneNumberField in your models.

django Python programming
Read