i am trying to calculate the new_balance
when a user withdraw any amount from thier main balance
.
i am trying to perform this operation when the form is being submitted but i do not know if this is the perfect way to perform this operations. This is what i am trying to achive.
def withdrawal_request(request):
...
if request.method == "POST":
form = WithWithdrawalRequestForm(request.POST)
if form.is_valid():
new_form = form.save(commit=False)
new_form.user = request.user
if new_form.amount > all_earning:
messages.warning(request, "You cannot withdraw more than what is in your wallet balance.")
return redirect("core:withdrawal-request")
elif pending_payout > new_balance:
messages.warning(request, "You have reached your wallet limit")
return redirect("core:withdrawal-request")
else:
new_form.save()
new_balance = new_balance - new_form.amount
messages.success(request, f"Withdrawal Request Is Been Processed...")
return redirect("core:withdrawal-request")
else:
form = WithWithdrawalRequestForm(request.POST)
context = {
"form":form,
"new_balance":new_balance,
}
return render(request, "core/withdrawal-request.html", context)
I was actually missing one more line
else:
new_form.save()
request.user.profile.main_all_earning = main_all_earning - new_form.amount
request.user.profile.save()
messages.success(request, f"Withdrawal Request Is Been Processed...")
return redirect("core:withdrawal-request")
else:
new_form.save()
new_balance = new_balance - new_form.amount
messages.success(request, f"Withdrawal Request Is Been Processed...")
return redirect("core:withdrawal-request")
else:
form = WithWithdrawalRequestForm(request.POST)
context = {
"form":form,
"new_balance":new_balance,
}
return render(request, "core/withdrawal-request.html", context)
You're returning at the end of the upper else
statement, so you can't reach the line where you update your context
. In Python a function exits after returning.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.