Format an un-decorated phone number in django?

后端 未结 5 801
我在风中等你
我在风中等你 2021-01-02 13:01

I\'ve got a DB chock full o\' phone numbers as strings, they\'re all formatted like 1112223333, I\'d like to display it as 111-222-3333 in my django template

I know

5条回答
  •  既然无缘
    2021-01-02 13:28

    This is quite a bit belated, but I figured I'd post my solution anyway. It's super simple and takes advantage of creating your own template tags (for use throughout your project). The other part of this is using the parenthesis around the area code.

    from django import template
    register = template.Library()
    
    def phonenumber(value):
        phone = '(%s) %s - %s' %(value[0:3],value[3:6],value[6:10])
        return phone
    
    register.filter('phonenumber', phonenumber)
    

    For the rest of your project, all you need to do is {{ var|phonenumber }}

提交回复
热议问题