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?