Can you say, is this a bad code for registration in Django? Do I need to fix something or use a pip instead? (I am learning)

def register(request):

    error_messages = {}

    if request.POST:
        form = UserCreationForm(request.POST)

        if form.is_valid():
            new_user = form.save()

            user = authenticate(username=request.POST["username"], password=request.POST["password1"])
            login(request, user)

            return redirect('index')
            form = UserCreationForm()
            return redirect('register')

    return render(request, 'account/register.html',
            {'title': 'Registration', 'error_messages': error_messages})

1 Answer 1


From what you can find on the docs you are pretty close. I may change a couple things a little just to be on the safe side:


user = authenticate(username=request.POST["username"], password=request.POST["password1"])


user = authenticate(username=form.cleaned_data['username'], form.cleaned_data['password1'])

The first way is not wrong, but using validated data on forms is a better practice than using the raw post request. Is better to be consistent.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.