Home > Back-end >  The difference between
The difference between

Time:07-05

What is the difference between:

request.post.get('blog','')

AND

request.post.get('blog')

I am not able to figure out what is the difference between these two and what they return.

CodePudding user response:

request.post.get('blog','') will return an empty string if the value is missing in the POST, as you defined it as default in the .get()

request.post.get('blog') will return None if the value is missing in the POST, as you did not defined any default value in .get()

Docs: https://docs.python.org/3/library/stdtypes.html#dict.get

CodePudding user response:

The REQUESR.POST is like a normal dictionary in python, so when you trying to access an element in first example,

You telling python "return the value of BLOG and if it not there just return an empty string", and you can change the default value (the empty str) to whatever you want.

In the second example you didn't provide any default value so if there is no BLOG key in the request it will raise an ERROR, and won't return anything.

  • Related