Python 当用户提交某个内容时返回实例属性

Python 当用户提交某个内容时返回实例属性,python,html,django,Python,Html,Django,现在我正在尝试建立一个电子商务网站。我的问题是,我无法计算用户在当前购买上花费的总金额,我不知道如何使用当前产品对象的价格,相反,我只得到了None。我知道可能有一个简单的答案 这是我的模型.py: from __future__ import unicode_literals from django.contrib.auth.models import User from django.db import models from django import forms, views from

现在我正在尝试建立一个电子商务网站。我的问题是,我无法计算用户在当前购买上花费的总金额,我不知道如何使用当前产品对象的价格,相反,我只得到了
None
。我知道可能有一个简单的答案

这是我的模型.py:

from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.db import models
from django import forms, views
from django.db.models import Sum

class ExtendedProfile(models.Model):
    user = models.OneToOneField(User)
    amount_spent = models.DecimalField(max_digits=6, decimal_places=2, default=0)

    #@classmethod tells the class to act on itself instead of an instance of itself
    @classmethod
    def total_amount(cls):
        #returns a dictionary
        return cls.objects.all().aggregate(total=Sum('amount_spent'))

class RevenueInfo(models.Model):
    #here we access the dictionary
    user_total = ExtendedProfile().total_amount()['total']
    total_amount_spent = models.DecimalField("Total User Revenue", max_digits=6, decimal_places=2, default=user_total)

class Product(models.Model):
     category = models.CharField(max_length=100)
     name = models.CharField(max_length=100)
     description = models.TextField()
     #photo = models.ImageField()
     price_CAD = models.DecimalField(max_digits=6, decimal_places=2)
     quantity = models.DecimalField(max_digits=2, decimal_places=0, null=True, editable=True)
以下是我的观点

def product_page(request):
    all_products = Product.objects.all()
    quantity_forms = QuantityForms(request.POST)
    quantity = request.POST.get('amount')
    grand_total = RevenueInfo.user_total
    if quantity > 0:
        return HttpResponse(Product().price_CAD)
    return render(request,'tracker/product_page.html', {'all_products':all_products, 'quantity_forms':quantity_forms})
这是模板:

{% load staticfiles %}
<!DOCTYPE html>
<!DOCTYPE html>
<html>
{% for object in all_products %}
<h3><a href="{{ object.get_absolute_url }}">{{ object.name }}</a></h3>
<p>{{ object.description }}</p>
<p>{{ object.price_CAD }}</p>
<form method='POST'>
{% csrf_token %}
{{ quantity_forms.amount }}<button>Buy</button>
</form>
{% endfor %}
</html>
{%load staticfiles%}
{所有_产品%中对象的%}
{{object.description}}

{{object.price_CAD}}

{%csrf_令牌%} {{quantity_forms.amount}}购买 {%endfor%}

现在,我只是想至少返回用户按“购买”按钮购买的产品的正确数量。然后从那里我将计算购买的总金额

几个问题,首先,您只是返回新创建产品的
价格\u CAD

if quantity > 0:
    return HttpResponse(Product().price_CAD)
如果允许空白,则始终为无;如果不允许空白,则始终为零

您应该返回与用户关联的产品。但是,您的模型没有显示这种关联是否存在


其他问题包括,您没有通过
request.user
抓取登录用户,您直接从
request.POST
获取数据,这是不安全的。

通过执行
Product().price\u CAD
创建新的
产品,并访问它的
price\u CAD
,它显然是空的。通过这行代码,你想实现什么?返回HttpResponse(Product().price\u CAD)
?是的,你说得比我好。我知道我不会退回与用户相关的产品。但我不知道如何做到这一点,第一步是在相关模型之间添加
ForeignKey
s。假设您已经这样做了,下一步就是执行相关的对象查找。虽然我通常不赞成在收到答案后对问题进行重大修改。如果你添加了相关的模型,我会很高兴发表更多的评论。嘿,非常感谢你的帮助,我对模型进行了编辑。现在我只为
ExtendedProfile
类建立了
OneToOne
关系。我是否需要为每个
用户的每个
产品
属性制作一个ForeignKey?您不应该有一个购买表吗?理想的情况是通过购买表在产品和用户之间建立多个关系。Tbh在这方面我还是新手,我知道什么是通过购买表,只是不确定我是否需要。从来没有听说过购物桌