How to solve ValueError at /register in django

I am trying to create a sign up with Django but after filling the form, I keep getting this ValueError at /register as an error requesting that the username be set. I am new to Django and I don't seem to understand what is expected Below are my codes for signup form, the views.py and the resulting error respectively

Signup form:

{% for message in messages %}
<h5>{{message}}</h5>
{% endfor %}
<form method="POST" action="register" name="registerForm">
    {% csrf_token %}
    <p>Username:</p>
    <input type="text" name="username "/>
    <p>Email:</p>
    <input type="email" name="email "/>
    <p>Password:</p>
    <input type="password" name="password"/>
    <p>Repeat Password:</p>
    <input type="password" name="confirm_password"/><br>
    <input type="submit"/>
</form>

views.py

def register(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        email = request.POST.get('email')
        password = request.POST.get('password')
        confirm_password = request.POST.get('confirm_password')
        
        if password==confirm_password:
            if User.objects.filter(username=username).exists():
                messages.info(request, 'Username Taken')
                return redirect('register')
            elif User.objects.filter(email=email).exists():
                messages.info(request, 'Email Taken')
                return redirect('register')
            else:
                user = User.objects.create_user(username=username, email=email, password=password)
                user.save()
                
                return redirect('/')
                
        else:
            messages.info(request, 'Password does not match')
            return redirect('register')
    else:
        return render(request, 'register.html')

Error: ValueError at /register The given username must be set Request Method: POST Request URL: http://127.0.0.1:8000/register Django Version: 4.1.3 Exception Type: ValueError Exception Value:
The given username must be set Exception Location: C:\Users\User\Envs\myapp\lib\site-packages\django\contrib\auth\models.py, line 144, in _create_user Raised during: myapp.views.register Python Executable: C:\Users\User\Envs\myapp\Scripts\python.exe Python Version: 3.10.4 Python Path:
['C:\Users\User\Desktop\Python_journey\djangoTut\myproject', 'C:\Users\User\AppData\Local\Programs\Python\Python310\python310.zip', 'C:\Users\User\AppData\Local\Programs\Python\Python310\DLLs', 'C:\Users\User\AppData\Local\Programs\Python\Python310\lib', 'C:\Users\User\AppData\Local\Programs\Python\Python310', 'C:\Users\User\Envs\myapp', 'C:\Users\User\Envs\myapp\lib\site-packages'] Server time: Tue, 22 Nov 2022 07:17:35 +0000

I think you are doing wrong here.

Change this:

action="register"

To this:

action="{% url 'register' %}" #You need to write name of this register route.

OR

Simply you can try this way:

action="/register/"

The problem is inside your signup.html:

{% for message in messages %}
<h5>{{message}}</h5>
{% endfor %}
<form method="POST" action="/register/" name="registerForm">
    {% csrf_token %}
    <p>Username:</p>
    <input type="text" name="username "/>
    <!-- blank space in this input -->
    <p>Email:</p>
    <input type="email" name="email "/>
    <!-- blank space in this input -->
    <p>Password:</p>
    <input type="password" name="password"/>
    <p>Repeat Password:</p>
    <input type="password" name="confirm_password"/><br>
    <input type="submit"/>
</form>

You have blankspaces inside your input tag "name" value. So these values are not being passed to your view inside your request, you get a None value instead.

And when trying to retrieve an object with this value, the exception is raised.

User.objects.filter(username=username).exists()

I would like to recommend to retrieve your model by using a try/except to handle errors, or use the Django get-object-or-404 shortcut before checking for its existence.

Back to Top