Django: How to create a registration feauture in django without using the Django Usercreation Form?

Viewed 20

I am trying to allow users create an account from without using the from django.forms import UserCreationForm. I just want the users to use just the input field and i can grab unto what ever they are passing into the input field and create an account for them.

This is the django forms for creating users with UserCreationForm, how do i now do the same but without the UserCreationForm?

views.py

def RegisterView(request):
    if request.method == "POST":
        form = UserRegisterForm(request.POST or None)
        if form.is_valid():
            new_user = form.save()
            username = form.cleaned_data.get('email')

            messages.success(request, f'Account Created')
            new_user = authenticate(username=form.cleaned_data['email'],
                                    password=form.cleaned_data['password1'],)
            login(request, new_user)
            return redirect('index')
            


    elif request.user.is_authenticated:
        return redirect('index')
    else:
        form = UserRegisterForm()
    context = {
        'form': form,
    }
    return render(request, 'userauths/sign-up.html', context)


forms.py

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

class UserRegisterForm(UserCreationForm):

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

1 Answers

You can do this by creating your own user modelForm.

This will include multiple steps:

  1. validate form
  2. save form
  3. set password using user.set_password() method.
Related