Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MultiValueDictKeyError / request.POST

Tags:

python

django

I think I hav a problem at request.POST['title']

MultiValueDictKeyError at /blog/add/post/ "'title'" Request Method: GET Request URL: http://119.81.247.69:8000/blog/add/post/ Django Version: 1.8.2 Exception Type: MultiValueDictKeyError Exception Value:
"'title'" Exception Location: /usr/local/lib/python2.7/dist- packages/django/utils/datastructures.py in getitem, line 322 Python Executable: /usr/bin/python Python Version: 2.7.3

views.py

def add_post(request):
    entry_title = request.POST["title"]
    return HttpResponse('Hello %s' % entry_title)

write.html

<form method="POST" action="/blog/add/post/">
<p>
    <label for "title">Title</label>
    <input type="text" id="title" name="title" value="" />
</p>
<p>
    <label for 'category'>Category</label>
    <select id="category" name="category"></select>
</p>
<p>
    <label for 'tags'>Tags</label>
    <input type="text" id="tags" value="" />
</p>
<p>
    <textarea id="content" name="content"></textarea>
</p>
<p>
    <input type="submit" value="Write" />
</p>

like image 705
Kwang Avatar asked Jun 07 '15 16:06

Kwang


People also ask

What does request POST do in Django?

Django puts data in request. POST when a user submits a form with the attribute method="post" . Line 10: You retrieve the submitted value by accessing the data at the key "follow" , which you defined in your template with the name HTML attribute on your <button> elements.

What is mean by MultiValueDictKeyError?

Most of the case's this MultiValueDictKeyError occurred for missing key in the dictionary-like request object. Because dictionary is an unordered key, value pair “associative memories” or “associative arrays” In another word. request. GET or request.

How do I fix MultiValueDictKeyError?

To fix the Python Django MultiValueDictKeyError error, we use the dictionary get method to get the dict value and return a default if the dict key doesn't exist. to call get on request. POST to get the is_private dict value from the POST request payload. And if is_private wasn't sent, then we return False .

What is Is_private in Django?

The line is is_private = request.POST['is_private'] The problems lies within the form, the is_private is represented by a checkbox. If the check box is NOT selected, obviously nothing is passed. This is where the error gets chucked.


2 Answers

Change:

def add_post(request):
    entry_title = request.POST["title"]
    return HttpResponse('Hello %s' % entry_title)

to:

def add_post(request):
    entry_title = request.POST.get("title", "Guest (or whatever)")
    return HttpResponse('Hello %s' % entry_title)

and it won't throw a KeyError, but you should look at using Django's forms rather than pulling values directly from the POST data.

Alternatively, you can keep your existing code and simply check for the exception:

def add_post(request):
    try:
        entry_title = request.POST["title"]
    except KeyError:
        entry_title = "Guest"
    return HttpResponse('Hello %s' % entry_title)

but this is what .get() does internally already.

like image 109
Brandon Avatar answered Nov 15 '22 08:11

Brandon


I had the same problem, I discovered that I forgot to add "name=" text" " in my input type in my Html page..

like image 24
Ahmed Adewale Avatar answered Nov 15 '22 10:11

Ahmed Adewale