Authenticate User with Django 1.5

398 views
Skip to first unread message

Tom Christie

unread,
Dec 15, 2012, 5:33:58 PM12/15/12
to django...@googlegroups.com
I believe you'll need a custom authentication backend to tie in with your user model.

Take a look at the docs here:

https://docs.djangoproject.com/en/dev/ref/authbackends/

Here's an example of an auth backend that takes email/password instead of username/password:

https://github.com/dabapps/django-email-as-username/blob/master/emailusernames/backends.py

Hope that helps.

Russell Keith-Magee

unread,
Dec 15, 2012, 6:45:04 PM12/15/12
to django...@googlegroups.com

Hi Tom,

You've missed an important detail here: he's talking about Django 1.5 and a custom User model. A custom authentication backend isn't required; the ModelBackend should adapt to any well-defined User model.

Yours,
Russ Magee %-)


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/csl1KBONlr4J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Russell Keith-Magee

unread,
Dec 15, 2012, 6:52:50 PM12/15/12
to django...@googlegroups.com


On Sat, Dec 15, 2012 at 7:18 PM, <sebastie...@gmail.com> wrote:
Hi, i've an authenticate problem with Django 1.5
All informations are herehttp://stackoverflow.com/questions/13883539/authenticate-with-django-1-5 but i'll resume the situation :

I've a custum user model which looks like :

class User(AbstractBaseUser):
    email = models.EmailField(unique=True)
    activation_key = models.CharField(max_length=255)
    is_active = models.BooleanField(default=False)
    is_admin = models.BooleanField(default=False)

    objects = UserManager()
   
    USERNAME_FIELD = 'username'

With a form, i can register users, and all is correct.
Now, i'd like to log the registred user with :
class LoginForm(forms.Form):
    email = forms.EmailField()
    password = forms.CharField(
            label="Password",
            widget=forms.PasswordInput()
    )

The corresponding view is :

def login(request):
    form = LoginForm()
    if request.method == 'POST':
        form = LoginForm(request.POST)
        email =  request.POST['email']
        password = request.POST['password']
        user = authenticate(username=email, password=password)
        if user is not None:
            if user.is_active:
                login(user)
            else:
                message = 'disabled account, check validation email'
                return render(
                        request,
                        'account-login-failed.html',
                        {'message': message}
                )

    return render(request, 'account-login.html', {'form': form})


The probleme is that user = authenticate(username=email, password=password) gives me None as return.
According to the doc, authenticate takes an usersname, not an email as arg. But how can i use authenticate because my User model desn't support Username.
Is there a solution with Django 1.5 ?

Well, no, there isn't a special Django 1.5 solution -- what you've described here (calling authenticate() with the email address as the username argument) should be all you need to do.

My initial guess would be that you're not calling the right authenticate method; you haven't included your imports, but based on the fact that you're calling "login()", in a view called "login()", I'm going to guess there might be some import conflicts going on. I'd try starting in a shell trying to manually authenticate a user - if: 

>>> from django.contrib.auth import authenticate
>>> authenticate(username='te...@example.com', password='s3kr1t')

works, but your view doesn't, then the problem will be with imports. If it doesn't work, then the probably will probably be with the list of authentication backends -- I'm guessing the ModelBackend isn't being installed correctly. However, without more details, it's hard to tell.

Yours,
Russ Magee %-)

Ian Foote

unread,
Dec 15, 2012, 8:12:14 PM12/15/12
to django...@googlegroups.com
On 15/12/12 11:18, sebastie...@gmail.com wrote:
> Hi, i've an authenticate problem with Django 1.5
> All informations are
> herehttp://stackoverflow.com/questions/13883539/authenticate-with-django-1-5
> but i'll resume the situation :
>
> I've a custum user model which looks like :
>
> class User(AbstractBaseUser):
> email = models.EmailField(unique=True)
> activation_key = models.CharField(max_length=255)
> is_active = models.BooleanField(default=False)
> is_admin = models.BooleanField(default=False)
>
> objects = UserManager()
>
> USERNAME_FIELD = 'username'
>
<snip>
>
>
> The probleme is that user = authenticate(username=email,
> password=password) gives me None as return.
> According to the doc, authenticate takes an usersname, not an email as
> arg. But how can i use authenticate because my User model desn't support
> Username.
> Is there a solution with Django 1.5 ?
>

Have you tried changing USERNAME_FIELD to 'email'?

Ian

Tom Christie

unread,
Dec 16, 2012, 2:21:55 PM12/16/12
to django...@googlegroups.com
> A custom authentication backend isn't required; the ModelBackend should adapt to any well-defined User model.

Thanks Russ, I hadn't realized the ModelBackend tied in nicely with those changes.


> Have you tried changing USERNAME_FIELD to 'email'? 

+1, looks like that's the OP's issue.

Russell Keith-Magee

unread,
Dec 16, 2012, 6:35:04 PM12/16/12
to django...@googlegroups.com
Well spotted Ian -- that's clearly the problem. Sebastian; ignore my previous advice :-)

Yours,
Russ Magee %-) 
Reply all
Reply to author
Forward
0 new messages