What is the difference between
request.POST.get('sth')
and
request.POST['sth']
Did not find the similar question, both work the same for me, suppose I can use them separately but maybe I am wrong, that is why I am asking. Any ideas?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
request.POST['sth']will raise aKeyErrorexception if'sth'is not inrequest.POST.request.POST.get('sth')will returnNoneif'sth'is not inrequest.POST.Additionally,
.getallows you to provide an additional parameter of a default value which is returned if the key is not in the dictionary. For example,request.POST.get('sth', 'mydefaultvalue')This is the behavior of any python dictionary and is not specific to
request.POST.These two snippets are functionally identical:
First snippet:
Second snippet:
These two snippets are functionally identical:
First snippet:
Second snippet:
These two snippets are functionally identical:
First snippet:
Second snippet: