0

I want to partially display information from django database. For example:

There is a phone number in the database - +33547895132. In html i want to display - +3354*****2

models.py:

class Product(models.Model):
    number = models.CharField(max_length=25)
    num = models.CharField(max_length=7, blank=True)

    def couple(self, *arg, **kwarg):
        self.num = self.number[:6]
        super().couple()

this does not work

Edo Akse
  • 4,051
  • 2
  • 10
  • 21

1 Answers1

0

If you want to return a string with the same length as the original, the below does that.

Your requested output string has 1 less character.

tel = '+33547895132'

def get_masked(input: str, maxpre: int=5, maxpost: int=1, maskchr: str='*') -> str:
    """
    Returns a masked string based on the input values

            Parameters:
                    input   (str): the input string
                    maxpre  (int): how much characters from the start to show
                    maxpost (int): how much characters at the end to show
                    maskchr (str): the charcter used to mask

            Returns:
                    (str):         masked string
    """
    # check to see if we can actually mask this much
    if len(input) < maxpre + maxpost:
        return input
    else:
        fillwidth = len(input) - maxpost
        return f'{input[:maxpre]:{maskchr}<{fillwidth}}{input[fillwidth:]}'


print(get_masked(tel, maxpre=3, maxpost=2))
# this prints:
'+33*******32'


print(get_masked(tel))
# this prints:
'+3354******2'

This answer was used as the base for the solution

Edo Akse
  • 4,051
  • 2
  • 10
  • 21