Reputation: 679
What does request.POST.get("a") return while "a" is the reference to following html
label for = "a">ADDED ITEMS:</label>
<input hidden type="number" name="a" id="a">
def myfunc():
if request.method == "POST":
a = request.POST.get("a");
obj = TransferedItems(item_id = a);
obj.save();
return HttpResponse("sent")
else:
return HttpResponse("form submission failed")
I am new at Django and I am unable to find what does request.POST.get return. Please guide me on my question.
Upvotes: 0
Views: 2866
Reputation: 3560
Django's request.POST
is a dictionary-like object, which means that its get()
method behaves exactly like the regular dict
's get()
method:
get(key[, default])
Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.
So, to answer your question, request.POST.get("a")
either returns the value of a
, which is always a string (although it can be an empty string!) or the value None
. The following code tests which of these possibilities is the case:
a = request.POST.get("a")
if a is None:
return HttpResponseBadRequest("No key `a` present in POST data")
if a == "":
return HttpResponseBadRequest("Empty string supplied for `a` in POST data")
TransferedItems.objects.create(item_id = a)
return HttpResponse("sent")
Upvotes: 1
Reputation:
It returns the value of the field with name a
that your sending from HTML. If the value doesn't exists, it returns None
Upvotes: 1