0
votes

I am getting the below-mentioned error while executing the code:-

MultiValueDictKeyError at /count/ 'textbox' Request Method: GET Request URL: http://127.0.0.1:8000/count/ Django Version: 2.1 Exception Type: MultiValueDictKeyError Exception Value: 'textbox' Exception Location: /Users/rajans/anaconda3/lib/python3.7/site-packages/django/utils/datastructures.py in getitem, line 79 Python Executable: /Users/rajans/anaconda3/bin/python Python Version: 3.7.0 Python Path: ['/Users/rajans/Documents/djangoprojects/wordcount', '/Users/rajans/anaconda3/lib/python37.zip', '/Users/rajans/anaconda3/lib/python3.7', '/Users/rajans/anaconda3/lib/python3.7/lib-dynload', '/Users/rajans/anaconda3/lib/python3.7/site-packages', '/Users/rajans/anaconda3/lib/python3.7/site-packages/aeosa'] Server time: Tue, 5 Feb 2019 06:39:06 +0000

Python


from django.http import HttpResponse
from django.shortcuts import render

def homepage(request):
    return render(request,'home.html')


def contact(request):
    return HttpResponse("<h2> This is a contact_us page</h2><br> You can write to [email protected] for any query")

def count(request):
    data=request.GET['textbox']
    data_list=split(data)
    data_len=len(data_list)
    return render(request,'count.html',{'length':data_len})
--------

html form:-

<h1>Word Count</h1><br>
This is the home page for the Word count.

<form action="{% url 'count' name%}">  <!--  "{% url 'count' %}". this will load the url even if the path is changed..it will look for the name count-->
    <textarea name="textbox" cols=40 rows="10"></textarea><br/>
    <input type="submit" value="count"/>

</form>

count.html

<h1>Counted</h1><br/>
The length is :- {{length}}
4
try data=request.GET.get('textbox')Ahtisham
why dont u getting error split is not defined? That is not right way to splitYugandhar Chaudhari

4 Answers

0
votes

you are using http://127.0.0.1:8000/count/ this url when ever this url call to function it will go with GET method in your function you are trying to access a "textbox" but in first time visit textbox parameter is not there so it is showing error

you need to modify your view function and write one condition

def count(request):
    data=request.GET.get('textbox', None)
    if data:
        data_list=split(data)
        data_len=len(data_list)
        return render(request,'count.html',{'length':data_len})
    else:
        return render(request,'add here your html page name where input form is there')
2
votes

Looks like you're not passing textbook variable as parameter in URL.

Try calling your URL as /count_url/?textbook="value that you want to pass"

Also, would suggest to use .get method as

data = request.GET.get('textbook', None)

if data:
    # do something
0
votes

Use the MultiValueDict's get method, you can also provide default value if textbox not present.

data=request.GET.get('textbox', None)
if data:
   #process
else:
   #process
0
votes

You have two problems here, first, you have to deal with the fact that (as the other answer states already) you might reach this view without a textbox key in your GET.

That you can fix as shown in the others answers:

data=request.GET.get('textbox', None)

But the one I'd worry about is the action attribute of your form:

<form action="{% url 'count' name%}">

with that, you're saying your count view receives another parameter, but that's not what we see in your view implementation:

def count(request):
    ...

So you should change your view to:

def count(request, name):
    data=request.GET.get('textbox', None)
    data_len = 0
    if data:
        data_list=split(data)
        data_len=len(data_list)
    return render(request,'count.html',{'length':data_len})

And your URL should also specify your view can receive another param:

re_path(r'count/(\w+)?', count, name=count)