为什么我总是收到一条消息,说资金不足,即使我提取的金额少于我的余额?



我打算向用户显示消息"余额不足";当他想提取的金额大于可用余额时,但即使他想提取金额较低,也会显示消息。这里到底出了什么问题?

视图

def create_withdrawal_view(request):
if request.method == 'POST':
withdraw_form = WithdrawalForm(request.POST)
if withdraw_form.is_valid():
investment = withdraw_form.save(commit=False)
if investment.balance < investment.amount:
messages.success(request, 'insufficient funds')
else:
investment.balance -= investment.withdraw_amount
investment.save()
messages.success(request, 'investment successful')
else:
withdraw_form = WithdrawalForm()
context = {'withdraw_form': withdraw_form}
return render(request, 'create-withdrawal.html', context)

我的型号

class Investment(models.Model):
PLAN_CHOICES = (
("Basic - Daily 2% for 180 Days", "Basic - Daily 2% for 180 Days"),
("Premium - Daily 4% for 360 Days", "Premium - Daily 4% for 360 Days"),
)
user = models.ForeignKey(
User, on_delete=models.CASCADE, null=True, blank=True)
plan = models.CharField(max_length=100, choices=PLAN_CHOICES, null=True)
deposit_amount = models.IntegerField(default=0, null=True)
basic_interest = models.IntegerField(default=0, null=True)
premium_interest = models.IntegerField(default=0, null=True)
investment_return = models.IntegerField(default=0, null=True)
withdraw_amount = models.IntegerField(default=0, null=True, blank=True)
balance = models.IntegerField(default=0, null=True, blank=True)
locked_balance = models.IntegerField(default=0, null=True, blank=True)
investment_id = models.CharField(max_length=10, null=True, blank=True)
is_active = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now=True, null=True)
due_date = models.DateTimeField(null=True)

我的模型表单

class InvestmentForm(forms.ModelForm):
class Meta:
model = Investment
fields = ['deposit_amount', 'plan']

class WithdrawalForm(forms.ModelForm):
class Meta:
model = Investment
fields = ['withdraw_amount']

我的urls.py


urlpatterns = [
path('dashboard/', views.list_investments, name='list-investments'),
path('create/', views.create_investment_view, name='create-investment'),
path('withdraw/', views.create_withdrawal_view, name='withdraw'),
]

相信您在这里使用了错误的数据:

def create_withdrawal_view(request):
if request.method == 'POST':
withdraw_form = WithdrawalForm(request.POST)
if withdraw_form.is_valid():
investment = withdraw_form.save(commit=False)
if investment.balance < withdraw_form.withdraw_amount:
messages.success(request, 'insufficient funds')
else:
investment.balance -= withdraw_amount.withdraw_amount
investment.save()
messages.success(request, 'investment successful')
else:
withdraw_form = WithdrawalForm()
context = {'withdraw_form': withdraw_form}
return render(request, 'create-withdrawal.html', context)

您必须验证表单withdraw_form.withdraw_amount的数据。而不是CCD_ 2。

但你也必须以某种方式从你需要提取的地方获得投资。我认为你需要获得pk,并做这样的事情:

investment = Investment.objects.get(pk=pk)

最新更新