Question

Perhaps the most interesting feature of Django 1.5 is the custom user that finally bids farewell to the outdated username.

There is an excellent nearly out-of-the-box example in the documentation to create a user where you would need only the email address instead of username.

There is however a small caveat. For some reason after a successful signup, I am not able to login the user automatically as it would have worked as described in Django 1.4 in here

In other words, this doesn't work for me:

user = authenticate(username=form.cleaned_data['email'], password=form.cleaned_data['password2'])
login(request, user)

The strange part is I am not even getting any error message in the debug console nor any warnings.

According to the out-of-the-box example though there is no custom backend defined. Only a CustomUser and CustomUsermanager. Hence I don't understand why I can't login the user manually.

btw I also have set the following in the settings:

AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',)

Any Django 1.5 expert around here?

Was it helpful?

Solution 2

I finally found the solution. After debugging in the internal django classes. The solution is embarrassingly simple.

user = authenticate(username=form.cleaned_data['email'], password=form.cleaned_data['password2'])
login(request, user)

My mistake was the wrong namespace for login(request, user)

Make sure you are using

from django.contrib.auth import login

And not by mistake:

from django.contrib.auth.views import login

everything else was correct in my settings.

OTHER TIPS

I just ran the example myself, and it did work.

Did you set the AUTH_USER_MODEL before running syncdb ?

If not, erase your DB and run the command again. Based on the linked example it should ask you your date of birth while creating the superuser if everything is configured correctly.

And by the way, the ModelBackend is the default AUTHENTICATION_BACKENDS, you don't have to set it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top