我收到以下错误:
unsupported operand type(s) for /: 'QueryDict' and 'float'
'amount' 中传递的值是 4.90,所以我很困惑为什么它不能 /。
class OrderForm(forms.Form):
amount = forms.FloatField()
credits = forms.FloatField()
def __init__(self, amount, *args, **kwargs):
super(OrderForm, self).__init__(*args, **kwargs)
self.fields['amount'].widget = forms.HiddenInput()
self.fields['amount'].initial = amount
total_credits = credit_total(amount)
self.fields['credits'].widget = forms.HiddenInput()
self.fields['credits'].initial = total_credits
我曾尝试使用 float() 但这不起作用。
校准函数
def credit_total(amount):
credit_total = amount / 0.049
return credit_total
我必须注意,在我第一次加载表单时,它可以工作......
cost_amount = float(request.POST['amount'])
form = OrderForm(amount=cost_amount)
但是,当我再次填写表单时,在发布时它不会....
if request.method == 'POST':
form = OrderForm(request.POST)
但是帐户现在在 POST 请求中。
也许我需要做这样的事情......
def __init__(self, amount=None, *args, **kwargs):
super(OrderForm, self).__init__(*args, **kwargs)
if amount is none then make amount from args = ammount????