1
votes

this is my views.py file

from django.shortcuts import render

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

def add(request):

val1 = int(request.GET['num1'])
val2 = int(request.GET['num2'])
res = val1 + val2

return render(request, "result.html",{'result': res})

when i run this code i am getting this error,

error

then i tried this code:

def add(request):

    val1 = int(request.GET.get(['num1']))
    val2 = int(request.GET.get(['num2']))
    res = val1 + val2

    return render(request, "result.html",{'result': res})

this error occur.

enter image description here

urls.py file

from django.urls import path 
from . import views

urlpatterns = [
    path("",views.home,name="home"),
    path("add", views.add, name="add")
]

home.html file

{% extends 'base.html' %}

{% block content %}

<h1> hello  {{name}} </h1>

<form action="add">

    {% csrf_token %}

    Enter 1st number : <input type="text" name="num1"><br>
    Enter 2nd number : <input type="text" name="num2"><br>
    <input type="submit">

</form>

{% endblock %}

Result.html file

{% extends 'base.html' %}

{% block content %}

    Result : {{result}}

{% endblock %}

when i try POST function then it give error which mention below

from django.shortcuts import render

# Create your views here.

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

def add(request):

    val1 = int(request.POST['num1'])
    val2 = int(request.POST['num2'])
    res = val1 + val2

    return render(request, "result.html",{'result': res})

MultiValueDictKeyError at /add 'num1' Request Method: GET Request URL: http://127.0.0.1:7000/add?csrfmiddlewaretoken=t6mVWboKI37vxW3zDbVmpnB3j3g89EzjFhikDDMrB4qVEfrNyqG5pebeXgDLAFE0&num1=3&num1=3 Django Version: 3.0.3 Exception Type: MultiValueDictKeyError Exception Value:
'num1' Exception Location: C:\Users\irtiza\AppData\Local\Continuum\anaconda3\lib\site-packages\django\utils\datastructures.py in getitem, line 78 Python Executable: C:\Users\irtiza\AppData\Local\Continuum\anaconda3\python.exe Python Version: 3.7.4

what should i do now to resolve this error. This is just a simple function to ADD two numbers.Kindly help me.

3
I think you need to use POST method instead of GETMeha Parekh
In your second attempt, try changing request.GET.get(['num1']) to request.GET.get('num1', 0)ybl
The problem is with how you are calling the url and passing argumentsEternal
basically i have a home.html file where i create the form and took values and then i have button which have a function to add both numbers. the problem is when i hit submit button it throws me this error. "MultiValueDictKeyError"Muhammad Irtiza
Please post your errors as code, not images or image links.Itamar Mushkin

3 Answers

1
votes

You are passing two times num1 in your url as below...

url - http://127.0.0.1:7000/add?csrf.......&num1=2&num1=3

Change it to blow...

url - http://127.0.0.1:7000/add?csrf.......&num1=2&num2=3

And get it by below...

def add(request):

    val1 = int(request.GET.get('num1'))
    val2 = int(request.GET.get('num2'))
    res = val1 + val2

    return render(request, "result.html",{'result': res})

And Change this line from your form as below...

Enter 2nd number : <input type="text" name="num2"><br>
0
votes

Remove square brackets. It is not required.

val1 = int(request.GET.get('num1'))
val2 = int(request.GET.get('num2'))
0
votes

Basically I was missing ( method = "post or GET" ) in form in the home.html

The changes I have made are:

home.html

{% extends 'base.html' %}

{% block content %}

<h1> hello  {{name}} </h1>

<form action="add" method="POST">

    {% csrf_token %}

    Enter 1st number : <input type="text" name="num1"><br>
    Enter 2nd number : <input type="text" name="num2"><br>
    <input type="submit">

</form>

{% endblock %}

views.py

from django.shortcuts import render

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

def add(request):

val1 = int(request.POST['num1'])
val2 = int(request.POST['num2'])
res = val1 + val2

return render(request, "result.html",{'result': res})

and then it works fine.