我正在 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 ```
/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 ```
任何形式的帮助将不胜感激,在此先感谢。
尝试为每个 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,它可能是空的。
request.POST