#forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class NewUserForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
def save(self, commit=True):
user = super(NewUserForm, self).save(commit=False)
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
#views.py
from django.contrib import messages
from django.shortcuts import render, redirect
from .forms import NewUserForm
def sign_up_view(request):
if request.method == 'POST':
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
messages.success(request, "Registration successful.")
return redirect('inventory:login')
messages.error(request, "Unsuccesful information. Invalid information")
form = NewUserForm()
return render(request, 'registration/signup.html', context={"register_form":form})