我正在创建一个基本的购物车应用程序,我可以在我的购物车中添加所有物品以填充数量和成本,但我正在尝试找到一种方法来创建总成本,并在顶部添加销售税那。显然,总线是问题,但我不确定我能做些什么来填充所有产品的价格和数量并将它们相乘。
{% for addtocart in userCart %}
<li>Quantity:{{addtocart.quantity}} <a href="/products/{{addtocart.product.id}}">{{addtocart.product.prodName}}</a> ${{addtocart.product.prodPrice}}(<a
href=/delItem/{{addtocart.id}}>Remove</a>)<br>
{% endfor %}
</ul>
{% else %}
<p> No products available </p>
{% endif %}
***<p>Total: {{addtocart.quantity}}"x"{{addtocart.product.prodPrice}}***
视图.py
def addtocart(request, prod_id):
if (request.method == 'POST'):
form = CartForm(request.POST)
if form.is_valid():
newComment = form.save()
newComment.session = request.session.session_key[:20]
newComment.save()
return HttpResponseRedirect('/products/' + str(newComment.product.id))
else:
form = CartForm( {'name':'Your Name', 'session':'message', 'product':prod_id} )
return render_to_response('Products/comment.html', {'form': form, 'prod_id': prod_id})
def userHistory(request):
userCart = Cart.objects.filter(session = request.session.session_key[:20])
return render_to_response('Products/history.html', {'userCart':userCart})
模型.py
from django.db import models
from django.forms import ModelForm
# Create your models here.
class prod(models.Model):
prodName = models.CharField(max_length=100)
prodDesc = models.TextField()
prodPrice = models.FloatField()
prodImage = models.ImageField(upload_to="userimages/")
def __unicode__(self):
return self.prodName
class Cart(models.Model):
session = models.CharField(max_length=100)
product = models.ForeignKey('prod')
quantity = models.IntegerField()
def __unicode__(self):
return self.name
class CartForm(ModelForm):
class Meta:
model = Cart
exclude = ('session')