如何在数据库中保存的HTML页面中向用户显示余额? [英] How to show balance to user in html page which is saved in database?
本文介绍了如何在数据库中保存的HTML页面中向用户显示余额?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已将余额分配给每个已注册并将该余额保存在数据库中的用户,但我无法在 html页面
中显示他们。也许我在 python
文件中也犯了一些错误。在 views.py
中,我评论了一些我虽然也使用过但仍未得到结果的代码值。
models.py
class Payment(models.Model):
金额= models.DecimalField(max_digits = 12,decimal_places = 2)
owner = models.ForeignKey(User,on_delete = models.CASCADE)
views.py
<从django.db.models导入pre>
从十进制导入的总和
十进制
def付款(请求):
total_amount = Payment.objects.filter(
owner = request.user)['total_amount']或小数('0.00')
for total_amount中的字段:
field.amount
context = {
'total_amount':total_amount
}
return render(request,'index.html',context)
HTML页面
< li style = float:right ;>您的余额:卢比。 {{total_amount}}< / li>
Python Shell
>>从users.models导入Payment
>> Payment.objects.all()
< QuerySet [<付款:付款对象(1)>,<付款:付款对象(2)>]>
包含内容的数据库图像
https://ibb.co/481nzqv
https://ibb.co/B4M1NTk
解决方案
尝试一下
来自django.db .models从django.contrib.auth.decorators导入Sum
并输入login_required
@ login_required
def付款(要求):
totalamount =付款。 objects.filter(
owner = request.user).aggregate(Sum('amount'))
context = {'total_amount':totalamount ['amount__sum']}
返回render(request,'index.html' ,上下文)
请参考此
I have given balance to each user who have signed up and saved that balance in database but I am not able to show them in html page
. Maybe I have made some mistakes in python
files also. In views.py
I have commented some code values which I used also but still didn't get the results. How to do it?
models.py
class Payment(models.Model):
amount = models.DecimalField(max_digits=12, decimal_places=2)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
views.py
from django.db.models import Sum
from decimal import Decimal
def payment(request):
total_amount = Payment.objects.filter(
owner=request.user)['total_amount'] or Decimal('0.00')
for field in total_amount:
field.amount
context = {
'total_amount': total_amount
}
return render(request, 'index.html', context)
HTML page
<li style="float: right;">Your Balance: Rs. {{total_amount}}</li>
Python Shell
>>> from users.models import Payment
>>> Payment.objects.all()
<QuerySet [<Payment: Payment object (1)>, <Payment: Payment object (2)>]>
Images of database which contains content
https://ibb.co/481nzqv
https://ibb.co/B4M1NTk
解决方案
try this
from django.db.models import Sum
from django.contrib.auth.decorators import login_required
@login_required
def payment(request):
totalamount = Payment.objects.filter(
owner=request.user).aggregate(Sum('amount'))
context = { 'total_amount': totalamount['amount__sum']}
return render(request, 'index.html', context)
refer this
这篇关于如何在数据库中保存的HTML页面中向用户显示余额?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文