python - /accounts/signup/ 'password' 处的 MultiValueDictKeyError
问题描述
我是 django python 的新手,我在注册过程中遇到了这个奇怪的错误。
请让我知道我在哪里做错了。
这是我在views.py中的代码:
if request.method=='POST':
#SignUp
if request.POST['password1'] == request.POST['password2']:
try:
user = User.objects.get(username = request.POST.get('username1'))
return render(request, 'accounts/signup.html', {'error':'username is already taken'})
except User.DoesNotExist:
user = User.objects.create_user(username=request.POST['username1'], password=request.POST['password'])
auth.login(request, user)
return redirect('home')
else:
#enter info
return render(request, 'accounts/signup.html')
如果user= request.POST.get('username1')
我输入,无论我输入什么,我都会收到“用户名已被占用”消息。
我的html代码:
<form method="POST" action="{% url 'signup' %}" class="form" autocomplete="off">
{% csrf_token %}
<div class="form__group">
<input type="text" placeholder="username" name="username1" class="form__input" id="username1" autocomplete="off"/>
</div>
<div class="form__group" autocomplete="off">
<input type="email" placeholder="email" name="email" class="form__input" autocomplete="off"/>
</div>
<div class="form__group">
<input type="password" placeholder="Password" name="password1" class="form__input" />
</div>
<div class="form__group">
<input type="password" placeholder="Confirm Password" name="password2" class="form__input" />
</div>
<input class="btn btn-primary" type="submit" value="signup!"/>
</form>
错误是:
MultiValueDictKeyError at /accounts/signup/
'password'
Request Method: POST
Request URL: http://127.0.0.1:8000/accounts/signup/
Django Version: 2.2.9
Exception Type: MultiValueDictKeyError
Exception Value:
'password'
Exception Location: C:\Users\HP\Desktop\django\zakevenv\lib\site-packages\django\utils\datastructures.py in __getitem__, line 80
Python Executable: C:\Users\HP\Desktop\django\zakevenv\Scripts\python.exe
Python Version: 3.7.4
Python Path:
['C:\\Users\\HP\\Desktop\\django\\producthunt-project',
'C:\\Users\\HP\\Desktop\\django\\zakevenv\\Scripts\\python37.zip',
'C:\\Users\\HP\\Desktop\\django\\zakevenv\\DLLs',
'C:\\Users\\HP\\Desktop\\django\\zakevenv\\lib',
'C:\\Users\\HP\\Desktop\\django\\zakevenv\\Scripts',
'C:\\Users\\HP\\Anaconda3\\Lib',
'C:\\Users\\HP\\Anaconda3\\DLLs',
'C:\\Users\\HP\\Desktop\\django\\zakevenv',
'C:\\Users\\HP\\Desktop\\django\\zakevenv\\lib\\site-packages']
Server time: Thu, 23 Jan 2020 15:20:06 +0000
我已经花了几个小时,但我仍然不知道我在哪里做错了:(。任何帮助将不胜感激。
解决方案
if request.method=='POST':
#SignUp
if request.POST['password1'] == request.POST['password2']:
try:
user = User.objects.get(username = request.POST.get('username1'))
return render(request, 'accounts/signup.html', {'error':'username is already taken'})
except User.DoesNotExist:
user = User.objects.create_user(username=request.POST['username1'], password=request.POST['password1'])
auth.login(request, user)
return redirect('home')
else:
#enter info
return render(request, 'accounts/signup.html')
错误:如果您查看您的 try 块,您正在尝试获取用户名,那么您没有通过此用户名执行任何操作,然后在渲染方法中您通过分配信息发送错误消息。因此,在模板文件上显示错误消息,因为您将其作为默认消息发送。
推荐阅读
- javascript - 如何删除数据表文件上传中的按钮
- c# - 一个 ControlTemplate 中的多个 ContentPresenter
- android - 从一个活动调用解密方法到适配器
- c# - 如何在 C# 中将 Html 代码转换为 Word 文档
- c++ - MSVC 不良程序集生成
- python - 如何在 django 中通过 ManyToManyField 组合查询集
- android - Ionic 5 ERR_AVD_HOME_NOT_FOUND:找不到有效的 Android AVD 主页。在 Windows 10 中
- python-3.x - 我怎样才能重写下面的代码来获得一个实际的数据集,而不是我得到的空数据框响应?
- python - Dask one-hot-encoding 不知道类别
- javascript - 运算符'>'不能应用于Angular 8中的类型'void'和'number'