开发者

Django, How authenticate user with first name and last name?

i want to authenticate users using firstname and lastname

This is the code i am using

user = auth.authenticate(first_name=firstname,last_name=l开发者_Go百科astname,password=password)

it keep coming up with NoneType: None

i have checked the firstname and lastname plus password seen to be correct?

what i am doing wrong? thanks


The difficulty here is that normally you'd handle this by creating a custom authentication backend that implements authenticate and get_user. However, the function signature for authenticate is:

def authenticate(self, username=None, password=None):

Everywhere in Django that would be calling this will be passing only 2 parameters, username and password. This means that using any of the generic authentication forms and things like the admin interface will break if this is done any other way.

The only work around I could see, and this is kind of sketchy, is if the username were to be typed as a single entry with a string "First Last" (delimited by a space) in place of the username. You could then separate it out and use that value...

(this is all untested, but you get the idea)

class FirstLastNameBackend(object):
    def authenticate(self, username=None, password=None):
        first, last = username.split(' ', 1)
        try:
            user = User.objects.get(first_name=first, last_name=last)
            if user:
                # Check if the password is correct
                # check if the user is active
                # etc., etc.
                return user
        except:
            pass
        return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except:
            return None

The django doc provides a lot of helpful details on doing a custom backend: User auth with custom backend

On a side note, something to be careful of is last names that have a space(s) in them, like "de la Cruz". If you specify 1 for maxsplit on the split function, you'll avoid this problem.


Building a little from @T. Stone's idea. Why not have them register with their First and Last name and you just concatenate them together and use that as their username?. And everytime you have them login you setup your view to combine the two fields again and use that string.

You won't be able to use some of the auto forms they can produce for you but that's not a big deal. I'd just combine the two strings, lowercase them and slap that as the username and do the same for every login instance.


You can use any parameters in backend authentication function, i.e.:

class FirstLastNameBackend(object):
  def authenticate(self, first_name=None, last_name=None, password=None):
    pass #your user auth code goes here

In order to authenticate user you call

user = auth.authenticate(first_name=firstname,
    last_name=lastname,
    password=password)

One drawback, however, that you'll need to implement your own log in form and this authentication won't be supported in admin interface.


I like the idea of not making users remember a username, but I think a better solution to that is to have their email address be their user name. Is it fair for you to assume in your specific application that no two users will have the same first and last name? If that's not a fair assumption, how will your system handle that?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜