Django显示 MultiValueDictKeyError at /myDetector/

发布于 2022-09-11 19:53:30 字数 3581 浏览 14 评论 0

我试过很多办法了 都没法解决 各位大哥们
帮忙看看
代码见下
mainDealer()函数
def mainDealer(request):

# post = request.POST.items()
print("rawTarget =", request.POST["rawTarget"])
target = reverse("myDetector:tempTest") + '?'
argName = request.POST["paramName"]
argPath = argName.split("?")[0]
argName = argName.split("?")[1]
# dealt by model
lmModel = Judger.readModelFromFile(name=Judger.getFileName(
    argName, "2018119", "lm"), obj_hook=Judger.lmFromFile)
cmModel = Judger.readModelFromFile(name=Judger.getFileName(
    argName, "2018119", "cm"), obj_hook=Judger.cmFromFile)
lmPredictRst = lmModel.predict(
    samples=[
        request.POST["paramValue"]],
    realRst=[])
cmPredictRst = cmModel.predict(
    samples=[
        request.POST["paramValue"]],
    realRst=[])
judgeRst = {"lm": lmPredictRst[0], "cm": cmPredictRst[0]}
if not judgeRst["lm"]:
    addNewMarkedValue(argName, request.POST["paramValue"], "lm", argPath)
if not judgeRst["cm"]:
    addNewMarkedValue(argName, request.POST["paramValue"], "cm", argPath)
#
request.session["argName"] = argName
request.session["argPath"] = argPath
request.session["argValue"] = request.POST["paramValue"]
request.session["ip"] = request.META['REMOTE_ADDR']
request.session["judgeRst"] = judgeRst
request.session["rawTarget"] = request.POST["rawTarget"]
target = target[:-1]
return HttpResponseRedirect(target)

app_name = 'myDetector'
urlpatterns = [

url(r'^$', views.mainDealer, name='mainDealer'),
url(r'^tempTest/$', views.tempTest, name='tempTest'),
url(r'^showModels/$', views.showModels, name='showModels'),
url(r'^delOneMarkedValue/$', views.dealMv),
url(r'^reTrain/$', views.reTrain),
url(r'^machineClassificationRst/$', views.readMLClassificationRst),
url(r'^dealMLClassificationRst/$', views.dealMLClassificationRst),
url(r'^xssIntoDb/$', views.xssIntoDb, name='xssIntoDb'),
url(r'^xssDetectorPage/$', views.xssDetectorPage, name='xssDetectorPage'),
url(r'^readXss/$', views.readXss, name='readXss'),
url(r'^xssStateChange/$', views.xssStateChange, name='xssStateChange'),

]

问题描述

MultiValueDictKeyError at /myDetector/
"'rawTarget'"
Request Method:
GET
Request URL:
http://localhost:8000/myDetector/
Django Version:
1.11.7
Exception Type:
MultiValueDictKeyError
Exception Value:
"'rawTarget'"
Exception Location:
D:ProjecttestSitevenvlibsite-packagesdjangoutilsdatastructures.py in __getitem__, line 85
Python Executable:
D:ProjecttestSitevenvScriptspython.exe
Python Version:
3.6.2
Python Path:
['D:\Project\malicoiusWebParamJudger\testSite',
'D:\Project\testSite\venv\Scripts\python36.zip',
'D:\Python\Python36\DLLs',
'D:\Python\Python36\lib',
'D:\Python\Python36',
'D:\Project\testSite\venv',
'D:\Project\testSite\venv\lib\site-packages',
'D:\Project\testSite\venv\lib\site-packages\setuptools-39.1.0-py3.6.egg',
'D:\Project\testSite\venv\lib\site-packages\pip-19.1.1-py3.6.egg',
'D:\Project\malicoiusWebParamJudger\testSite\myDetector',
'D:\Project\malicoiusWebParamJudger\testSite\myDetector\detectorTools',
'D:\Project\malicoiusWebParamJudger\testSite\myDetector\detectorTools\model',
'D:\Project\malicoiusWebParamJudger\testSite\myDetector\detectorTools\HereticJudger']
Server time:
Fri, 17 May 2019 09:58:33 +0800

问题出现的环境背景及自己尝试过哪些方法

尝试使用request.post.get() 但是没有用

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞慾 2022-09-18 19:53:30

有可能字典的key不存在,给它设定个默认值 default_value

可以参照下面的方法:

from django.utils.datastructures import MultiValueDictKeyError
try:
    rawTarget = request.POST['rawTarget']
except MultiValueDictKeyError:
    rawTarget = default_value

还有中方法,直接在字典里用get方法,取不到就赋默认值:

rawTarget = request.POST.get('rawTarget', default_value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文