每当我将数据输入数据库时,然后单击提交时,我都会遇到代码问题,这将导致错误Multyvaluekeyerror。我更改了表单值很多次,但是没有任何效果。请帮我解决这个问题.....非常感谢。
control.addOption({id: data.agentType, text: data.agentType});
MultiValueDictKeyError at /admin/criminals
'gender'
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/criminals
Django Version: 2.2.4
Exception Type: MultiValueDictKeyError
Exception Value:
'gender'
Exception Location: C:\Users\lenovo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\datastructures.py in __getitem__, line 80
Python Executable: C:\Users\lenovo\AppData\Local\Programs\Python\Python37-32\python.exe
Python Version: 3.7.4
Python Path:
['D:\\django project\\gadmin3',
'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32',
'C:\\Users\\lenovo\\AppData\\Roaming\\Python\\Python37\\site-packages',
'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages']
Server time: Mon, 7 Oct 2019 04:35:54 +0000
查看侧码:-
MAIN FORM CODE:-
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Gender</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<div name="gender" id="" class="btn-group" data-toggle="buttons">
<label class="btn btn-default" data-toggle-class="btn-primary"
data-toggle-passive-class="btn-default">
<input type="radio" name="" value="male" data-parsley-
multiple="gender"> Male
</label>
<label class="btn btn-primary" data-toggle-class="btn-primary"
data-toggle-passive-class="btn-default">
<input type="radio" name="" value="female" data-parsley-
multiple="gender"> Female
</label>
</div>
</div>
</div>
答案 0 :(得分:0)
cache-and-network:
是单选按钮(也可以在表单中添加名称),如果未选择任何选项,则不会得到任何输入。
为避免此问题,请使用gender
方法从get
请求中提取值。
POST
这将消除crgen = request.POST.get('gender', default_value)
问题
答案 1 :(得分:0)
您的输入中有名称,例如
<input type="radio" name="" value="female" data-parsley-
应该是
<input type="radio" name="gender" value="female" data-parsley-
您的代码可以进一步重构为:
def criminals(request):
if request.method=="POST":
s= Criminals(mname=request.POST['crname'],
ccrime=request.POST['crime'],
mage=request.POST['age'],
mheight= request.POST['height'],
mbody=request.POST['bodymark'],
mgender=request.POST['gender'])
s.save()
messages.success(request,"Criminal Added Successfully.")
return render(request,'criminal.html')
else:
return render(request,'criminal.html')