Created
May 4, 2010 16:46
-
-
Save ecarter/389637 to your computer and use it in GitHub Desktop.
Makes 10 digit phone number (US) pretty
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
### app.templatetags.default_filters.py | |
from django import template | |
register = template.Library() | |
@register.filter("phone_format") | |
def phone_format(value): | |
""" | |
Returns pretty formatted 10 digit US phone number as string | |
>>> phone_format('5558761234') | |
(555) 876-12345 | |
In the Template: | |
{{ object.phone|phone_format }} | |
""" | |
find = ['(',')','-','+',''] | |
p = '' | |
for n in find: | |
p = value.replace(n,'') | |
try: | |
phone = "(%s) %s-%s" % (p[0:3], p[3:6], p[6:10]) | |
except: | |
phone = value | |
return phone |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment