3

I am trying to make a website that registers users with first name, last name, email, and password. I have tried writing this form and using it in the views

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm


class RegisterationForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = (
            'first_name',
            'last_name',
            'email',
            'password1',
            'password2',
        )

    def save(self, commit=True):
        user = super(RegisterationForm, self).save(commit=False)
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.email = self.cleaned_data['email']

        if commit:
            user.save()
        return user

and using it in the views

from django.shortcuts import render, redirect
from Users.forms import RegisterationForm
from django.contrib.auth import authenticate, login




def home(request):
    return render(request, 'Users/index.html')


def register (request):
    if request.method == 'POST':
        form = RegisterationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/map_page/')
    else:
        form = RegisterationForm()
        args = {'form': form}
        return render(request, 'Users/index.html', args)

However when I do it this way and I fill it in I get an error of UNIQUE constraint failed: auth_user.username

I was wondering if there is a way to not use a username and have them signup and login with password and email.

I have tried making a backend and included it in the settings and that did not work. Then I have also tried using custom models and included that in the admin.py. I have also included it in the settings, and it gives me weird errors.

How can I register users with email instead of username, in a simple way?

AugustusCaesar
  • 175
  • 2
  • 14

2 Answers2

4

I think the most simple fix is that you set user.username = user.email.

Mostly this issue is caused by empty username, which is okay for the first user, but unique fail for the 2nd user.

Another option is that you remove the field username from User model.

https://docs.djangoproject.com/en/2.0/topics/auth/customizing/

class CustomUser(User): username = None

Bang Dao
  • 5,091
  • 1
  • 24
  • 33
2

The best you can do it's to create your own user and override the properties. A have used this template for all my Django projects:

class CustomUser(AbstractUser):

    USERNAME_FIELD = 'email'

    def natural_key(self):
        return dict(email=self.email)

CustomUser._meta.get_field('email')._unique = True
CustomUser._meta.get_field('email')._blank = False
CustomUser._meta.get_field('username')._unique = False
CustomUser._meta.get_field('username')._blank = True
CustomUser._meta.get_field('username')._null = True

This way you're forcing the email to become the unique username field and you're setting the username as nullable, blank and not unique.

Juan Urrego
  • 343
  • 2
  • 10