question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

templatetag for rendering phone number fields

See original GitHub issue

It would be great to have a template tag that replicated the behaviour of PhoneNumberInternationalFallbackWidget

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
typonautcommented, Nov 13, 2020

I’ve had a go at a template tag, perhaps someone could add it to the source, or correct it.

./phonenumber_field/templatetags/phonenumber_tags.py

from django.conf import settings
from django import template

from phonenumbers import PhoneNumberFormat
from phonenumbers.phonenumberutil import region_code_for_number
from phonenumber_field.phonenumber import PhoneNumber

register = template.Library()

@register.filter(name='pn_ifallback')
def pn_ifallback(value):

    """
        A filter to display phone numbers in the same format as displayed
        using the PhoneNumberInternationalFallbackWidget on a form.
        
        Usage:
        
            {{ template.var|pn_ifallback }}
    """
    
    region = getattr(settings, "PHONENUMBER_DEFAULT_REGION", None)

    if isinstance(value, PhoneNumber):
        number_region = region_code_for_number(value)
        if region != number_region:
            formatter = PhoneNumberFormat.INTERNATIONAL
        else:
            formatter = PhoneNumberFormat.NATIONAL
        return value.format_as(formatter)
    else:
        return value
1reaction
jcaguirre89commented, Sep 20, 2018

I added a property to the model and call that from the template instead of the field itself and works great

models.py

class Contact(models.Model):
    company = models.ForeignKey(Company, on_delete=models.CASCADE)
    timestamp = models.DateField(auto_now=True)    
    name = models.CharField(max_length=200)
    email = models.CharField(max_length=200)
    country = models.ForeignKey('countries.Country', on_delete=models.CASCADE, default=1)
    position = models.CharField(max_length=200, blank=True)
    phone = PhoneNumberField(blank=True, help_text='Must be in format +19999999999')
    notes = models.TextField(blank=True)
    main_contact = models.BooleanField(default=False)

    def __str__(self):
        return self.company.name

    @property
    def phone_formatted(self):
        if self.phone == '':
            return ''
        return self.phone.as_international

template

        <td>{{ person.name }}</td>
        <td>{{ person.position }}</td>
        <td>{{ person.email }}</td>     
        <td>{{ person.phone_formatted }}</td>   
Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - templatetag for rendering phone number fields -
It would be great to have a template tag that replicated the behaviour of PhoneNumberInternationalFallbackWidget. See More. View in GitHub. SOLVE ISSUE.
Read more >
Built-in template tags and filters - Django documentation
Loads a template and renders it with the current context. This is a way of “including” other templates within a template. The template...
Read more >
Template tags and filters - django-bootstrap3 - Read the Docs
Render a field. Tag name: bootstrap_field. Parameters: field. The form field to be ...
Read more >
Rendering a Django Form through a TemplateTag
I'm trying to render a form through a templatetag in django so that I can I can render the form on multiple pages....
Read more >
Create Custom Template Tags and Filters in Django - Pluralsight
The Django template language offers a wide range of built-in tags and filters for designing the presentation layer of your Django app.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found