mask_email filter

Mask an email address by removing most of the first portion and replacing it with "..."

For example. If you have a variable in your template context named email_address, and its value is "someguy@example.com"

{{ email_address|mask_email }}

will render as:

som...@example.com

If the part preceding @domain.com is shorter than 5 characters, only the first letter will be used, followed by "...". So if we have "erica@example.com"

{{ email_address|mask_email }}

will render as:

e...@example.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

fromdjangoimporttemplateregister=template.Library()defmask_email(email):"""Mask an email address."""name,domain=email.split('@')iflen(name)>5:# show the first 3 charactersmasked_name=name[:3]else:# just use the 1st charactermasked_name=name[0]return"%s...@%s"%(masked_name,domain)register.filter('mask_email',mask_email)