Created
January 12, 2017 11:26
-
-
Save umutcoskun/2bc10b16f74a55b950532e2782c53633 to your computer and use it in GitHub Desktop.
Django custom filter that formats phone numbers using 'phonenumbers' module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@register.filter(name='format_phonenumber') | |
def format_phonenumber(phone, country='TR'): | |
""" | |
Usage: | |
{{ user.phone|format_phonenumber }} | |
{{ user.phone|format_phonenumber: 'TR' }} | |
""" | |
import phonenumbers | |
return phonenumbers.format_number( | |
phonenumbers.parse(phone, country), | |
phonenumbers.PhoneNumberFormat.NATIONAL, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment