MultiValueDictKeyError at /signup

Question:

I am creating a login in page in django but facing the issue MultiValueDictKeyError at /signup
Below is views.py
”’

from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.contrib import messages


def home(request):
    return render(request, "authentication/index.html")


def signup(request):
    if request.method == "POST":
        uname = request.POST['uname']
        fname =request.POST['fname']
        lname = request.POST['lname']
        email = request.POST['email']
        pass1 = request.POST['pass1']
        pass2 = request.POST['pass2']

        myuser = User.objects.create_user(uname, email, pass1)
        myuser.first_name = fname
        myuser.last_name = lname

        myuser.save()

        messages.success(request, "Your account has been successfully created.")

        return redirect('signin')

    return render(request, "authentication/signup.html")


def signin(request):
    return render(request, "authentication/signin.html")


def signout(request):
    pass

”’

Below is the signup.html
”’

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Database</title>
</head>
<body>

        <h3>Sign Up</h3>
        <form action="/signup" method="post">
            {% csrf_token %}
            <label for="">Username</label>
            <input type="text" id="uname" placeholder="Create a username", Required>
            <br>

            <label for="">First Name</label>
            <input type="text" id="fname" placeholder="Enter First Name", Required>
            <br>

            <label for="">Last Name</label>
            <input type="text" id="lname" placeholder="Enter Last name", Required>
            <br>

            <label for="">Email</label>
            <input type="email" id="email" placeholder="Enter Email address", Required>
            <br>

            <label for="">Password</label>
            <input type="password" id="pass1" placeholder="Create a Password", Required>
            <br>

            <label for="">Confirm your password</label>
            <input type="password" id="pass2" placeholder="Confirm your password", Required>
            <br>

            <button type="submit">Sign Up</button>

        </form>

</body>
</html>

”’

Below is the Error Message

MultiValueDictKeyError at /signup

"'uname'"

Request Method:   POST
Request URL:  http://127.0.0.1:8000/signup
Django Version:   3.2.15
Exception Type:   MultiValueDictKeyError
Exception Value:  

"'uname'"

Exception Location:   /home/im-lp-1841/PycharmProjects/Database/venv/lib/python3.7/site-packages/django/utils/datastructures.py,

line 78, in getitem
Python Executable: /home/im-lp-1841/PycharmProjects/Database/venv/bin/python
Python Version: 3.7.12
Python Path:

['/home/im-lp-1841/PycharmProjects/Database',
 '/home/im-lp-1841/PycharmProjects/Database',
 '/usr/lib/python37.zip',
 '/usr/lib/python3.7',
 '/usr/lib/python3.7/lib-dynload',
 '/home/im-lp-1841/PycharmProjects/Database/venv/lib/python3.7/site-packages']

Server time:  Fri, 30 Sep 2022 06:31:12 +0000

Any kind of help will be appreciated, Thanks in advance.

Asked By: ISMAIL MULLA

||

Answers:

Try adding a name attribute to each HTML input element.
example:

<input type="text" id="uname" name="uname" placeholder="Create a username", Required>
<input type="text" id="fname" name="fname" placeholder="Enter First Name", Required>
...
...
...

The issue currently is that the key cannot be found, print out the request.POST, and it will likely be empty.

Answered By: nigel239