how can I choose option price from checkbox option and calculate the sum.

67 views
Skip to first unread message

rdyact

unread,
Jan 4, 2015, 12:55:02 AM1/4/15
to django...@googlegroups.com

I want to program to calculate sum of user sleclecion of options with checkbox.
Pls help me how to keep away from this suffer.
I summarize what I want to do in my attached ppt file.

How can I code for this in my viws.py & models.py each?


#Product&Choice Model - models.py

class Product(models.Model):
title = models.CharField(max_length=120)
description = models.TextField(null=True, blank=True)
price = models.DecimalField(decimal_places=0, max_digits=100, default=10000)

class Choice(models.Model):
product = models.ForeignKey(Product)
name = models.CharField(max_length=255)
price = models.DecimalField(decimal_places=0, max_digits=100, default=0)

# single.html

<p>Your Product Price : {{ product.price }}$</p><hr/>

{% for item in product.choice_set.all %}
<form method='POST' action=''>{% csrf_token %}<input type='checkbox' value='{{ item.id }}' name='choice'> {{ item.name }} : {{ item.price }}$ <br/>
{% endfor %}
<input type='submit' value='submit'>
</form><hr/>
~~
optioncheckbox.pptx

Collin Anderson

unread,
Jan 5, 2015, 10:45:49 PM1/5/15
to django...@googlegroups.com
Hi,

You probably need to use javascript to make the total price auto-update when you check or uncheck each box. Check out jQuery if you haven't.

Once you have that, then, yes, you'll want to re-calculate the price in django to be sure it's correct.

You could create an Order or Cart model that stores which (or how many of each) items have been select, and maybe the total price too.

Collin

rdyact

unread,
Jan 6, 2015, 3:04:46 AM1/6/15
to django...@googlegroups.com
Oh, thanks a lot, I'll try it anyway. ~~

2015년 1월 6일 화요일 오후 12시 45분 49초 UTC+9, Collin Anderson 님의 말:
Reply all
Reply to author
Forward
Message has been deleted
0 new messages