问题描述
我必须将 total_cost
变量设置为等于小计变量或来自产品类别的变量。但是,当我尝试从类中获取值时。它给了我一个 AttributeError: 'Product' object has no attribute '_Product__total_price'
的属性错误。我正在实施您应用促销代码的位置,然后它将从购物车中扣除总价。
这是 init.py
和使用的函数。
@app.route('/applyPromo/<string:id>/',methods=['GET','POST'])
def apply_promo(id):
promotions_dict = {}
db = shelve.open('promotions.db','w')
promotions_dict = db['Promotions']
total_cost = 100
hidden = True
applied = True
click = True
get_from_class = False
print(promotions_dict)
promotions = promotions_dict.get(UUID(id))
db = shelve.open('storage.db','r')
product_dict = db['products']
for key in product_dict:
product = product_dict[key]
total_cost = product.get_total_price()
print(total_cost)
#when user apply promo,promo will get deleted from the list/dictionary of promos they have.
if promotions["type"] == 1:
total_cost = total_cost - 10
hidden = False
print(f"Total Cost : {total_cost}")
promotions_dict.pop(UUID(id))
elif promotions["type"] == 2:
total_cost = total_cost - 5
hidden = False
print(f"Total Cost : {total_cost}")
promotions_dict.pop(UUID(id))
elif promotions["type"] == 3:
total_cost = (70/100)*total_cost
hidden = False
print(f"Total Cost : {total_cost}")
promotions_dict.pop(UUID(id))
elif promotions["type"] == 4:
total_cost = (80/100)*total_cost
hidden = False
print(f"Total Cost : {total_cost}")
promotions_dict.pop(UUID(id))
elif promotions["type"] == 5:
total_cost = (85/100)*total_cost
hidden = False
print(f"Total Cost : {total_cost}")
promotions_dict.pop(UUID(id))
else:
total_cost = (90/100)*total_cost
hidden = False
print(f"Total Cost : {total_cost}")
promotions_dict.pop(UUID(id))
db['Promotions'] = promotions_dict
db.close()
db.close()
print(promotions_dict)
session['promotion_applied'] = promotions["id"]
return render_template("generatePromo.html",total_cost=total_cost,applied=applied,promotions_dict=promotions_dict,hidden=hidden,promotions=promotions,get_from_class=get_from_class,click=click)
@app.route('/shopping_cart')
def shopping_cart():
# session.clear()
error = None
cart_items = []
quantity_list = []
subtotal = 0
db = shelve.open('storage.db','r')
product_dict = db['products']
db.close()
for products in session:
item = product_dict.get(products)
cart_items.append(item)
if None in cart_items:
cart_items.remove(None)
quantity_list.append(session[products])
if products in quantity_list:
quantity_list.remove(products)
for i in range(len(cart_items)):
cart_items[i].set_purchased_quantity(quantity_list[i])
# set total price for single item
item_total = int(cart_items[i].get_price()) * int(cart_items[i].get_purchased_quantity())
cart_items[i].set_total_price(item_total)
# set total price of all items in cart
subtotal += item_total
print('QTY LIST',quantity_list)
print('CART',cart_items)
if not cart_items:
error = "Cart Is Empty"
return render_template('shoppingcart.html',products_list=cart_items,error=error,subtotal=subtotal)
这是产品类。
from uuid import uuid4
class Product:
def __init__(self,name,price,quantity,color,vase,remarks):
self.__product__id = str(uuid4())
self.__name = name
self.__price = price
self.__quantity = quantity
self.__color = color
self.__vase = vase
self.__remarks = remarks
self.__purchased_quantity = 0
self.__total_price = 0
def get_product_id(self):
return self.__product__id
def get_name(self):
return self.__name
def get_price(self):
return self.__price
def get_quantity(self):
return self.__quantity
def get_color(self):
return self.__color
def get_vase(self):
return self.__vase
def get_remarks(self):
return self.__remarks
def get_image(self):
return self.__image
def get_purchased_quantity(self):
return self.__purchased_quantity
def get_total_price(self):
return self.__total_price
def set_name(self,name):
self.__name = name
def set_price(self,price):
self.__price = price
def set_quantity(self,quantity):
self.__quantity = quantity
def set_color(self,color):
self.__color = color
def set_vase(self,vase):
self.__vase = vase
def set_remarks(self,remarks):
self.__remarks = remarks
def set_image(self,image):
self.__image = image
def set_purchased_quantity(self,purchased_quantity):
self.__purchased_quantity = purchased_quantity
def set_total_price(self,total_price):
self.__total_price = total_price
这是错误的回溯。
解决方法
不应该使用前导双下划线,因为您使用它们来定义类的属性 - 单下划线是常规的。使用前导双下划线的问题是解释器“破坏”了这些属性的名称以避免继承的微妙问题,但我相信,您正在看到问题的根源。这是一个 good read on the subject。