The Python Oracle

Logic behind Form(request.POST or None)

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Future Grid Looping

--

Chapters
00:00 Logic Behind Form(Request.Post Or None)
00:32 Accepted Answer Score 16
01:04 Thank you

--

Full question
https://stackoverflow.com/questions/3825...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django

#avk47



ACCEPTED ANSWER

Score 16


The use of or in this case does not evaluate to True or False, but returns one of the objects.

Keep in mind that or is evaluated from left to right.

When the QueryDict request.POST is empty, it takes a Falsy value, so the item on RHS of the or operation is selected (which is None), and the form is initialized without vanilla arguments (i.e. with None):

form = MyModelForm()

Otherwise, when request.POST is not empty, the form is initialized with the QueryDict:

form = MyModelForm(request.POST)