简体   繁体   English

django 上的 MultiValueDictKeyError

[英]MultiValueDictKeyError on django

home.html主页.html

 {% extends 'base.html'%}
 {%block content%}
 <h1>hello word{{name}}</h1>

 <form action="add">
 <label>fname</label><input type="text" name="fn">
 <label>lname</label><input type="text"name="ln">
 <input type="submit" >
 </form>
 {%endblock%}

base.html底座.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body bgcolor="red">

{%block content%}

{%endblock%}

<p>hello dhiraj how are you</p>
</body>

result.html结果.html

{% extends 'base.html'%}
{%block content%}

Result is :{{result}}
{%endblock%}

urls.py网址.py

from django.urls import path
from .import views

urlpatterns = [

  path('',views.index ,name="index"),
  path('add',views.add,name="add")
 ]

views.py视图.py

def index(request):
    return render(request,'home.html',{'name':'dhiraj'})
   

def add(request):
    val1=request.GET["fn"]
    val2=request.GET["ln"]
    res=val1+val2
    return render(request,'result.html',{'result':res})

getting error is得到错误是

Quit the server with CTRL-BREAK.
Internal Server Error: /add
Traceback (most recent call last):
 File "C:\Users\Dhiraj Subedi\fpro\ero\lib\site-packages\django\utils\datastructures.py", line 76, in 
 __getitem__
 list_ = super().__getitem__(key)
KeyError: 'fn'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "C:\Users\Dhiraj Subedi\fpro\ero\lib\site-packages\django\core\handlers\exception.py", line 47, 
 in inner
 response = get_response(request)
 File "C:\Users\Dhiraj Subedi\fpro\ero\lib\site-packages\django\core\handlers\base.py", line 181, in 
 _get_response
 response = wrapped_callback(request, *callback_args, **callback_kwargs)
 File "C:\Users\Dhiraj Subedi\fpro\fproject\fapp\views.py", line 11, in add
 val1=request.GET["fn"]
 File "C:\Users\Dhiraj Subedi\fpro\ero\lib\site-packages\django\utils\datastructures.py", line 78, 
in __getitem__
raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'fn'
[09/May/2021 17:14:11] "GET /add? HTTP/1.1" 500 73579
Internal Server Error: /add
Traceback (most recent call last):
 File "C:\Users\Dhiraj Subedi\fpro\ero\lib\site-packages\django\utils\datastructures.py", line 76, in 
__getitem__
list_ = super().__getitem__(key)
KeyError: 'fn'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\Dhiraj Subedi\fpro\ero\lib\site-   
packages\django\core\handlers\exception.py", line 47, in inner
 response = get_response(request)
 File "C:\Users\Dhiraj Subedi\fpro\ero\lib\site-packages\django\core\handlers\base.py", line 181, in 
 _get_response
 response = wrapped_callback(request, *callback_args, **callback_kwargs)
 File "C:\Users\Dhiraj Subedi\fpro\fproject\fapp\views.py", line 11, in add
val1=request.GET["fn"]
  File "C:\Users\Dhiraj Subedi\fpro\ero\lib\site-packages\django\utils\datastructures.py", line 78, 
 in __getitem__
raise MultiValueDictKeyError(key)

 django.utils.datastructures.MultiValueDictKeyError: 'fn'
[09/May/2021 17:14:12] "GET /add? HTTP/1.1" 500 73579

Due to the above mentioned error, my form is not getting displayed.由于上述错误,我的表单没有显示。 Please help me in identifying and resolving the issue.请帮助我识别和解决问题。 i am relly trouble on this issue please help me I am really trouble on this issue thank you form are not display on screen why this problem occurs please help me我在这个问题上很麻烦请帮助我我在这个问题上真的很麻烦谢谢你的表格没有显示在屏幕上为什么会出现这个问题请帮助我

It means that you don't have the key fn in your GET .这意味着您的GET中没有密钥fn

You should use method get() of MultiValueDict :您应该使用MultiValueDictget()方法:

def add(request):
    val1=request.GET.get("fn", "")
    val2=request.GET.get("ln", "")
    res=val1+val2
    return render(request,'result.html',{'result':res})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM