我正在創建一個基本購物車應用程序,我可以在我的購物車中獲取所有項目以填充數量和成本,但我試圖找到一種方式即我可以創建一個總成本,並在其上添加銷售稅。顯然,總的路線是問題,但我不確定我能做什麼來填充所有產品的價格和數量,並將它們相乘。計算總成本並將購物車添加銷售稅
{% 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}}***
views.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})
models.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')
對不起聽起來啞巴,但我更多地看情況1,它不是直截了當(我在一個非常初學者水平)。在我的views.py裏面,我不確定要放什麼 – 2012-04-25 04:08:45
你可以發佈你的視圖功能嗎?這會讓我更容易向您展示 – cberner 2012-04-25 04:18:11
頂部的編輯示例,感謝您的幫助 – 2012-04-25 04:29:58