Logic behind Form(request.POST or None)

前端 未结 1 1028
感动是毒
感动是毒 2021-01-06 18:19

What\'s the logic behind request.POST or None? I haven\'t seen such thing in Python projects except Django.

Since or operator returns

相关标签:
1条回答
  • 2021-01-06 18:29

    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)
    
    0 讨论(0)
提交回复
热议问题