简体   繁体   中英

Using diff of two strings to suggest a unique string (Python 2.7)

In a Python/Django website I maintain, users keep unique usernames at sign up. While registering, if a username isn't available, they have to guess another one.

Sometimes users have to contend with multiple "username already exists" messages before they're able to sign up.

I want to ameliorate this issue via suggesting a username based upon the already used username they currently put in . Can someone illustrate a neat Python solution for this?


I haven't tried anything yet. But I was thinking what would work is taking the current nickname the user wants, and then somehow doing an ordinal based diff with 4-5 neighboring nicknames from the DB (these I can easily query).

The diffs that are found can then somehow be used to guess an available nickname for the user, which is also sufficiently based on the one they already wanted. Something like that. Being a neophyte, I'm still trying to wrap my head around a viable solution.

If you want to have 5 suggestions and the user only provides a username he likes to use you could do the following.

Just start a counter from 1 to 100, append that number to the username and check if it is in the database. If not then save that suggestion in a list.

If that list has 5 entries, show them to your user to choose from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM