How to prevent usernames from containing @/./+/-/_.?

后端 未结 3 1916
故里飘歌
故里飘歌 2021-01-27 09:38

I\'m using django-allauth, and for some reason the username default allows:

\"letters, digits and @/./+/-/_.\"

How can I ensure usernames are strictly alphanumer

3条回答
  •  攒了一身酷
    2021-01-27 09:56

    I haven't used django and certainly don't know about its authorisation mechanisms, but I know that in plain python, if you wanted to carry this out, you could simply sanitise input by doing:

    newUserName = ''.join(ch for ch in oldUserName if ch.isalnum())
    

    Essentially, I'm looping through each character and am appending them to the 'clean' username if the character is alphanumeric.

    In your function, if the username doesn't comply with the alphanumeric restrictions, execute the statement above (place it under the if).

提交回复
热议问题