Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7522817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:37:24+00:00 2026-05-30T02:37:24+00:00

My question is easier to explain with an example: I have a phone number

  • 0

My question is easier to explain with an example:

I have a phone number stored in my database as a string of numbers. Lets sat that the field is called phone, and it is inside a model called Business.

So, to print the phone number in a template, after creating the business var in the view I would use:

{{ business.phone }}

This will display the string of numbers like 2125476321

What is the best way to pretty print this phone number like (212) 547-6321? Is there any method that I can use inside the model?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:37:26+00:00Added an answer on May 30, 2026 at 2:37 am

    If the formatting of the phonenumbers doesn’t change you can write your own function to format the phonenumber (see this). A better approach would be to use a 3rd party library to do it for you, such as python-phonenumbers. The easiest approach is to do something like this:

    import phonenumbers
    class Business(models.Model):
        phone = ...
    
        def formatted_phone(self, country=None):
            return phonenumbers.parse(self.phone, country)
    

    in the template

     # We can't pass parameters from the template so we have to hope that python-phonenumbers guesses correctly
     {{ business.formatted_phone }}
    

    Alternatively (or concurrently) write a custom template filter to format the phonenumber. It would look like:

    {{ business.phone|phonenumber }} or {{ business.phone|phonenumber:"GB" }} 
    

    and written:

    import phonenumbers
    @register.filter(name='phonenumber')
    def phonenumber(value, country=None):
       return phonenumbers.parse(value, country)
    

    If you are only going to use the formatting in the template, write a template filter. If you think you will need the formatting in other aspects of your app, for example when listing the phonenumbers in the admin, write it in the model (but lose the ability to pass parameters to the function)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think, it is easier explain using an example. Let's take a class that
It might be easiest to explain the question with an example. I have a
Interview question- Often its pretty easier to debug a program once you have trouble
Question Can I build a image database/library that has an e-commerce style checkout system
I have a quick question that I'm sure some of you would have an
In order to make this question easier to describe I have provided the following
The same for all other atomic objects? It's easier to explain the question for
In relation to my last question ( Is there an easier way of passing
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Skip to bottom for question, but first, a little context. So I have been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.