Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7863079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:21:55+00:00 2026-06-02T23:21:55+00:00

I am creating a basic shopping cart application, I can get in my cart

  • 0

I am creating a basic shopping cart application, I can get in my cart all of the items to populate with the quantity and cost, but I am trying to find a way thats I can create a total cost, and adding sales tax on top of that. Clearly the total line is the issue, but I am not sure what I could do to populate price and quantity for all products and multiply them together.

{% 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:21:56+00:00Added an answer on June 2, 2026 at 11:21 pm

    There are two ways you can do this:

    1. compute the total in your view, and pass it in as a separate variable
    2. define a custom template tag that does multiplication (Django doesn’t have one by default)

    (1) in more detail:

    In your view:

    def userHistory(request):
        userCart = Cart.objects.filter(session = request.session.session_key[:20])
        totalCost = 0
        for item in userCart:
            totalCost += item.quantity * item.product.prodPrice
        return render_to_response('Products/history.html', {'userCart':userCart,
            'totalCost': totalCost})
    

    Then in your template:
    <p>{{totalCost}}</p>

    (2) in more detail:

    First make a new file my_app/templatetags/my_tags.py (also put an __init__.py file in this directory, so that Python knows it’s a package)

    from django import template
    register = template.Library()
    
    @register.filter 
    def multiply(arg1, arg2): 
        return float(arg1) * float(arg2)
    

    Then in your template add {% load my_tags %} at the top, and you’ll be able to use {{addtocart.quantity|multiply:addtocart.product.prodPrice}}

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a basic image browsing application using Silverlight. Depending on the user's
My current class is planning on creating a basic networked game, but we have
The basic setup is classic - you're creating a Windows Forms application that connects
I'm creating a basic database application in WPF, and I have started using the
I'm creating a basic COM component so I can practice creating them. I'm exporting
Trying to follow this tutorial Walkthrough: Part 1 - Creating a Basic Project System
I'm a desktop application developer. I'm creating a basic website from which to sell
I'm creating a basic jquery plugin but the extend doesn't seem to work.. I'm
I am creating a basic search function for an order cart. I have a
I am new to rails and I am creating a basic blog application. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.