MultiValueDictKeyError / request.POST
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.
I had the same problem, I discovered that I forgot to add "name=" text" "
in my input type
in my Html page..