表单背后的逻辑(request.POST或无) [英] Logic behind Form(request.POST or None)
问题描述
request.POST或无
背后的逻辑是什么?除了Django,我在Python项目中都没有看到过这种东西。
What's the logic behind request.POST or None
? I haven't seen such thing in Python projects except Django.
由于或
运算符返回 True 或 False 值,如果 request.POST
不是 None
, Form
知道并接受post作为参数吗?
Since or
operator returns True or False values, how is it possible that if request.POST
isn't None
, the Form
knows it and takes post as an argument?
form = MyModelForm(request.POST or None)
实际上,结果应为 Form(True)
如果 request.POST
不是 None
,否则为 Form(False)
。
In fact, the result should be Form(True)
if request.POST
isn't None
, otherwise Form(False)
.
它如何工作?
推荐答案
在这种情况下,使用或
不能评估为 True
或 False
,但返回其中一个对象。
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.
当QueryDict request.POST
为空时,它的值为Falsy,因此或操作的> RHS (即 None
),然后选择t表格初始化时没有 vanilla 参数(即与 None
):
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()
否则,当 request.POST
不为空时,该表单用QueryDict初始化:
Otherwise, when request.POST
is not empty, the form is initialized with the QueryDict:
form = MyModelForm(request.POST)
这篇关于表单背后的逻辑(request.POST或无)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!