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

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

我想部分显示django数据库中的信息。例如:
数据库中有一个电话号码-+33547895132。在html中我想显示-+3354****2
models.py:

  1. class Product(models.Model):
  2. number = models.CharField(max_length=25)
  3. num = models.CharField(max_length=7, blank=True)
  4. def couple(self, *arg,**kwarg):
  5. self.num = self.number[:6]
  6. super().couple()

这行不通

llew8vvj

llew8vvj1#

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

  1. tel = '+33547895132'
  2. def get_masked(input: str, maxpre: int=5, maxpost: int=1, maskchr: str='*') -> str:
  3. """
  4. Returns a masked string based on the input values
  5. Parameters:
  6. input (str): the input string
  7. maxpre (int): how much characters from the start to show
  8. maxpost (int): how much characters at the end to show
  9. maskchr (str): the charcter used to mask
  10. Returns:
  11. (str): masked string
  12. """
  13. # check to see if we can actually mask this much
  14. if len(input) < maxpre + maxpost:
  15. return input
  16. else:
  17. fillwidth = len(input) - maxpost
  18. return f'{input[:maxpre]:{maskchr}<{fillwidth}}{input[fillwidth:]}'
  19. print(get_masked(tel, maxpre=3, maxpost=2))
  20. # this prints:
  21. '+33*******32'
  22. print(get_masked(tel))
  23. # this prints:
  24. '+3354******2'

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

展开查看全部

相关问题