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?
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:in the template
Alternatively (or concurrently) write a custom template filter to format the phonenumber. It would look like:
and written:
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)