Skip to content

Instantly share code, notes, and snippets.

@ecarter
Created May 4, 2010 16:46
Show Gist options
  • Save ecarter/389637 to your computer and use it in GitHub Desktop.
Save ecarter/389637 to your computer and use it in GitHub Desktop.
Makes 10 digit phone number (US) pretty
### 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