Django: how to save form and perform math operation with along side saving the form in django?

i am trying to save a form, and after i want to get the new_balance and update the value, i have a function that would save a form, and the form have a field amount, so what i want is this, if i save the form i want to get the new_balance and substract the new_form.amount from the balance, it works but doesn't seems to be saving the new_balance when i refresh my page it shows this alert The page that you're looking for used information that you entered. Returning to that page might cause any action you took to be repeated. Do you want to continue?.

views.py


@login_required
def withdrawal_request(request):
    user = request.user
    profile = Profile.objects.get(user=user)
    total_investment = PurchasedPackage.objects.filter(paid=True, user=request.user).aggregate(Sum("investment_package__price"))['investment_package__price__sum']
    bonus_earning = profile.earning_point
    total_ref_earning = profile.referral_point
    indirect_ref_earning = profile.indirect_ref_earning
    daily_login_earning = profile.daily_login_earning
    bonus_point = profile.bonus_point
    social_share_points = profile.social_share_points
    
    pending_payout = WithdrawalRequest.objects.filter(user=request.user, status="pending").aggregate(Sum("amount"))['amount__sum']
    if pending_payout == None:
        pending_payout = 0
        
    total_payout = WithdrawalRequest.objects.filter(user=request.user, status="settled").aggregate(Sum("amount"))['amount__sum']

    try:
        all_earning = total_investment + bonus_earning + total_ref_earning + bonus_point + social_share_points + indirect_ref_earning + daily_login_earning
    except:
        all_earning =  bonus_earning + total_ref_earning + bonus_point + social_share_points + indirect_ref_earning + daily_login_earning


    try:
        new_balance = total_investment + bonus_earning + total_ref_earning + bonus_point + social_share_points + indirect_ref_earning + daily_login_earning
    except:
        new_balance = bonus_earning + total_ref_earning + bonus_point + social_share_points + indirect_ref_earning + daily_login_earning

    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 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:
                try:
                    new_balance = total_investment + bonus_earning + total_ref_earning + bonus_point + social_share_points + indirect_ref_earning + daily_login_earning
                except:
                    new_balance = bonus_earning + total_ref_earning + bonus_point + social_share_points + indirect_ref_earning + daily_login_earning

                new_form.save()
                new_balance = new_balance - new_form.amount
                messages.success(request, f"Withdrawal Request Is Been Processed... You would get a bank alert soon")
                return render(request, "core/withdrawal-request.html", {"new_balance":new_balance})
                

    else:
        form = WithWithdrawalRequestForm(request.POST)
        

    context = {
            "form":form,
            "new_balance":new_balance,
            
        }
    return render(request, "core/withdrawal-request.html", context)

the error is probably occuring at the line that writes

new_form.save()
new_balance = new_balance - new_form.amount

What is the best way to fix this problem?

Back to Top