简体   繁体   中英

django all-auth case insensitive username

I'm using Django AllAuth and was making a simple profile page for the users of the app. That is reachable by typing in a user's username.

# urls.py
path('<str:username>/', UserDetail.as_view(), name='user-detail'),

# views.py
class UserDetail (DetailView):
    template_name = 'dashboard/user.html'
    model = User

    def get_object(self):
        return get_object_or_404(User, username=self.kwargs['username'])

My thought is that it would work like twitter out of the gate. A user creates an account username like MyUsername which looks nice case-sensitive and is searchable case-insensitive. So if someone searched a user they could type in the username case-insensitive and it would always pull up the right user just with the nicer looking case-sensitive style.

That's when I realized a user with username: MyUsername as supposed to myusername is actually 2 different users. Naive for me to think otherwise I suppose.


Django AllAuth does have ACCOUNT_PRESERVE_USERNAME_CASING (=True) that I could implement but that would just make it so all usernames are lowercase while I was looking to just make usernames case-insensitive unique while still showing the nicer case-sensitive version.

What would be the best approach to handle make the username case-insensitive unique but still show the case-sensitive format to the users?

I would suggest ACCOUNT_PRESERVE_USERNAME_CASING = False and keep a "displayable" version of username in separate field (set on signup). If a username is changed, both names should be then updated as well (one internal for auth handling, the other for displaying).

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