微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MultiValueDictKeyError at /signup

如何解决MultiValueDictKeyError at /signup

尝试为每个 HTML 输入元素添加一个名称属性。例子:

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

当前的问题是找不到密钥,打印出来request.POST,它可能是空的。

解决方法

我正在 django 中创建登录页面,但在 /signup 处遇到 MultiValueDictKeyError 问题 下面是 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

’‘’

下面是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>

’‘’

以下是错误消息

/signup 处的 MultiValueDictKeyError

```py
“‘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,
```

第 78 行,在getitem Python 可执行文件:/home/im-lp-1841/PycharmProjects/Database/venv/bin/python Python 版本:3.7.12 Python 路径:

```py
[‘/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
```

任何形式的帮助将不胜感激,在此先感谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。