如何部分显示来自django数据库的信息?

gk7wooem  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(305)

我想部分显示django数据库中的信息。例如:
数据库中有一个电话号码-+33547895132。在html中我想显示-+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()

这行不通

llew8vvj

llew8vvj1#

如果要返回与原始字符串长度相同的字符串,请执行以下操作。
请求的输出字符串少了1个字符。

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'

这个答案被用作解决方案的基础

相关问题