[Django]-Django all-auth Email Required

5đź‘Ť

âś…

The solution to this was simple, it was just a configuration issue. I added these into the settings file:

ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = True
ACCOUNT_SIGNUP_FORM_CLASS = 'accounts.forms.SignupForm'
👤Callum

14đź‘Ť

This is an old question but in case it helps someone in the future, here is what the django-allauth states in its documentation:

ACCOUNT_AUTHENTICATION_METHOD (=”username” | “email” | “username_email”)
Specifies the login method to use – whether the user logs in by entering their username, e-mail address, or either one of both.

And

ACCOUNT_USERNAME_REQUIRED (=True)
The user is required to enter a username when signing up. Note that the user will be asked to do so even if ACCOUNT_AUTHENTICATION_METHOD is set to email. Set to False when you do not wish to prompt the user to enter a username.

So in your settings.py you could do this:

ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'

For more information about django-allauth configuration read here:
django-allauth documentation – configuration

👤GregC

1đź‘Ť

First, you should use ModelForm to save and object instance with your form data or you can just use built in UserCreationForm more detils here:

class RegisterForm(UserCreationForm):

    class Meta:
        model = UserProfile
        fields = ("email", "password1", "password2")

In your models.py file you should specify email field as username field. check this out

And finally login part. You should write your own custom login view to let users use their emails to login. In your urls put something like this:

(r'^email-login/', 'app.views.email_login', name="login"),

and views.py:

from django.contrib.auth import authenticate, login

def email_login(request):
    email = request.POST['email']
    password = request.POST['password']
    try:
        get_user = UserProfile.objects.get(email=email)
    except:
        get_user = None

    user = authenticate(username=get_user, password=password)
    if user is not None:
        if user.is_active:
            login(request, user)

I didn’t test the code but once i’ve solved my problem with this way. So these may give you some ideas to how to do that. And i believe you can find most of answers you need in this page

Comment if you need any extra info.

👤alioguzhan

1đź‘Ť

I agree with the accepted answer by Callum, apart from a typo (ACCOUNT_USERNAME_REQUIRED should be False). I don’t have enough points to leave my contribution in the form of a comment, however. The following works for Django 1.7.

In settings.py:

# Sign IN settings
ACCOUNT_AUTHENTICATION_METHOD = 'email'

# Sign UP settings
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
👤woodduck

Leave a comment