I am trying to make a custom registration form in Django, using HTML and CSS and not Django's form.as_p
. I have the following code in views.py:
def register(request):
if request.POST:
username = request.POST['username']
email = request.POST['email']
password = request.POST['password']
password_confirm = request.POST['password-confirm']
if(valid_form(username, email, password, password_confirm)) {
#create the new user
} else {
#send some error message
}
return render(request, 'index.html')
I have my own function valid_form to check if the form fields entered by the user are valid. However, I am not sure how I can create the new user using a custom User Model. In all of the code examples regarding registration forms I have seen something like this:
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect('main-page')
else:
form = UserCreationForm()
return render(request, 'users/register.html', {'form': form})
Where form.save()
is used to create the new user. I have the following model for a user in models.py:
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
id = models.AutoField(primary_key=True)
username = models.CharField(max_length=200, null=True)
email = models.EmailField(max_length=70, null=True)
How can I create a new CustomUser after validating form data? Any insights are appreciated.