Is there any filter in Django to display asterisk(*) instead of text

女生的网名这么多〃 提交于 2019-12-11 20:11:20

问题


I am eager to know whether any filter is available for displaying all the text as * like this

mytext = 'raja'

{{ mytext|password }} should show ****

How can we do this. plz help


回答1:


Easy. Do this:

{% for char in mytext %}*{% endfor %}

That said, can I ask you where you are displaying the password? Usually passwords are not displayed on screen. If you want to display it in a form you can use a PasswordInput widget.

As @Ars said it is a bad idea to reveal the length of the password. You might want to display a random number of asterisks instead.




回答2:


is this really a password? Then it seems like a bad idea -- do you want to reveal that the password is 4 characters long? Just print 4 (or 5 or whatever) asterisks straight in the template always.

Otherwise, I wouldn't bother with a filter. Simply pass in a string of asterisks through the context:

mytext = 'raja'
ctx = Context({'mytext': '*' * len(mytext)})
t = Template('password: {{ mytext }}')
s = t.render(ctx)


来源:https://stackoverflow.com/questions/3819260/is-there-any-filter-in-django-to-display-asterisk-instead-of-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!