Am trying to create a user request from an existing model object but it wont work. Even the ajax on the html page keep reloading.
Here's my model
class Users_Balanc(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
Amount = models.FloatField(max_length=4, default=0.0)
Profit = models.FloatField(max_length=30, default=0.0)
Bonus = models.FloatField(max_length=30, default=10.0)
Referral_Bonus = models.FloatField(max_length=30, default=0.00)
Deposit = models.FloatField(max_length=30, default=0.00)
Withdrawals = models.FloatField(max_length=30, default=0.00)
def __str__(self):
return f'{self.user.username} Users_Balanc'
And my view
@login_required(login_url='/login')
def withdrawals(request):
total = Users_Balanc.objects.all()
#return render(request, 'dashboard/withdrawals.html', {'total': total})
if request.method == 'POST':
Amount = request.POST['Amount']
username = request.GET.get['username']
total = Users_Balanc.objects.all()
if (total.Amount <= Amount):
New_Request = Request.objects.create(value=Amount, user=username)
New_Request.save
messages.info(request, 'Successful')
return HttpResponse('Request sent sucessful')
else:
return HttpResponse('Insufficient funds')
else:
return render(request, 'dashboard/withdrawals.html', {'total': total})
Html form
<form id="post-form">
{% csrf_token %}
<div >
<h5 >Amount</h5>
<input placeholder="TRX Amount" type="number" name="Amount" required>
<input type="hidden" name="username" id="username" value="{{username}}"/>
</div>
<div >
<h5 >Enter withdrawal password</h5>
<!--<input placeholder="Password" type="text" name="otpcode">-->
<small >If you forgot your password, you can contact admin</small>
</div>
<div >
<input type='submit' value="Complete Request"/>
</div>
It returns an nothing at all after submit button is clicked
For more clarification, am using ajax post request method on the html page to return the response
CodePudding user response:
There's a few things that could be going wrong here:
First off - you are looking for for both a GET and a POST. try:
Amount = request.POST['Amount']
username = request.POST['username']
Second,
total = Users_Balanc.objects.all()
This can return multiple objects, so total.Amount doesn't mean anything - it won't add them for you. If a user only has one account, you might try
total = Users_Balanc.objects.get(user__username = username)
then total.Amount will work.
If a user can have multiple accounts (which is indicated by the foriegn key 'user') you might want to try something like
user_accounts= Users_Balanc.objects.filter(user__username = username).aggregate(total_amount = Sum('total'))
This will 1) filter all the Users_Balanc objects that belong to one user then 2) add all the 'total' fields of those objects to produce user_accounts.total_amount so you can:
if (user_accounts.total_amount <= Amount):
And this will work if the user has one or many accounts - though it doesn't check if there is enough money in a single account if the user has many accounts.