So I’m working on a ecommerce project with Python and Django but I had some issues.
I want to change my total price that’s after the quantity products, my total price that’s up and my total items also that’s up because they’re static, u can see in pic :
in class OrderItem I add : @property
def get_total(self):
total = self.product.price * self quantity
return total
then I go to my template -----> cart.html and I change the static total price of $40
to this :
${{item.get_total}}
and everything is alright with those changes but then I do changes as below
in class Order I add : @property
def get_cart_total(self):
orderitems = self.orderitem_set.all()
total = sum([item.get_total for item in orderitems])
return total
@property
def get_cart_items(self):
orderitems = self.orderitem_set.all()
total = sum([item.quantity for item in orderitems])
return total
then I go to views.py and do this change :
from context = {‘items’:items} ----> context = {‘items’:items, ‘ordder’:order}
then do those changes in cart.html :
Items: 3
------>
Items: {{order.get_cart_items}}
Total: $43
----->
Total: ${{order.get_cart_total}}
So from changes that I do from class Order and below nothing changes on runserver !
I can’t add views.py and models.py images bc site didn’t let me as a new member but if needed I will share !
What you don’t want to do is share those files as images.
What you do want to do is to copy the contents of those files as text, and include them in your post between lines consisting of three backtick (`) characters.
example:
def function(parm1, parm2):
pass
Notice how it retains the formatting and highlights syntax.
On another note, I’m not understanding what the problem is that you’re trying to describe. Please include the details of what you’re expecting to see happen along with what’s happening. Also include any error messages that may be displayed in your console window in which you’re running runserver.
as you see in my picture above, the section ‘Total: $43’ and ‘Items: 3’ I want to change them depending on what price has the item and how much items a costumer has selected !
now the total price that is after quantity section as in the photo I can change that depending on the price and quantity of items by doing this in class OrderItem :
@property
def get_total(self):
total = self.product.price * self quantity
return total
then I go to template -------> cart.html and change this :
$40
to this
${{item.get_total}}
by doing those changes the total price that is $40 after the quantity section is depending now on how much and what items is selected by costumer!
but when I do changes as described above to change ‘Total: $43’ and ‘Items: 3’ none of them changes and also nothing is showing on my runserver (blank) !
Ok, I’m going to make a guess here and say that what I think you want to have happen is to have the user update the quantity field in the form, and have that show up as calculating a new total.
For this to happen, one of two things generally needs to occur:
The user has to click an “update” button, or otherwise trigger an event that will send the updated quantities to the server, for the server to re-render the page (or portion of the page) to display the new value.
You have to supply some JavaScript to be included in that page to capture the change event on that field to have the JavaScript update the total.
Always remember that templates are rendered on the server - anything that is done in the browser needs to be sent to the server if the server is going to know about it to perform any calculations or updates.