Kwang
Kwang

Reputation: 211

MultiValueDictKeyError / request.POST

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>

Upvotes: 6

Views: 55904

Answers (6)

med benzekri
med benzekri

Reputation: 603

I had the same problem,but when i changed return HttpResponseRedirect('') to

return HttpResponseRedirect('/') it worked

Upvotes: 0

Smriti Agrawal
Smriti Agrawal

Reputation: 1

For accessing Files in POST method this could be because you might have missed encrypting the files in form tag of the HTML file i.e.-

{form action="upload" method="POST" enctype="multipart/form-data"}
                                    ^^^^^^^^

is required to avoid MultiValueDictError.

Upvotes: 0

chpawankr
chpawankr

Reputation: 11

In Django project, I was facing the same problem, I did a mistake in url.py

wrong

path('support/',views.**support**,name='support'),

path('verifyDB/',views.**support**,name='verifyDB'),

correct one

path('support/',views.**support**,name='support'),

path('verifyDB/',views.**verifyDB**,name='verifyDB'),

so, check your path in view.py maybe there is a mistake.

Upvotes: -1

Ahmed Adewale
Ahmed Adewale

Reputation: 3133

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

Upvotes: 4

Brandon Taylor
Brandon Taylor

Reputation: 34583

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.

Upvotes: 16

alTus
alTus

Reputation: 2215

As your traceback says: Request Method: GET. So your POST dict is obviously empty and thus you get your KeyError.

Upvotes: 0

Related Questions