所以在我的代码中,
if ...:
if form.is_valid():
order_created = create_order(request,item)
order_number = order_created.id
if order_number:
....
context = {'total':order_created.total}
当我运行它时,我得到了unboundlocal error,local variable 'order_created' referenced before assignment
。我环顾四周,learnt
实际上我无法访问该块之外的块内的变量。我还了解到我不应该使用global statement
. 我还能如何访问order_created
,我可以在 if 块之外声明它,但这并不能验证订单。