Skip to content Skip to sidebar Skip to footer

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:8

Solution 1:

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.


Solution 2:

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


Solution 3:

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


Solution 4:

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.


Solution 5:

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.


Post a Comment for "Multivaluedictkeyerror / Request.post"