Im trying to start using session in django, Im trying to see the data in my session but im just getting the object reference not the data
console:
<cart.cart.Cart object at 0x7f6994753160>
views.py
def cart_view(request):
cart = Cart(request)
print(cart)
if request.user.is_authenticated:
return render(request, 'cart/cart_page.html')
else:
return redirect('account:login')
cart.py
class Cart:
def __init__(self, request):
self.session = request.session
cart = self.session.get('cart')
if not cart :
cart = self.session['cart'] = {} #cria a sessão cart
self.product = cart
CodePudding user response:
you can override the __str__
method on anyclass to control how it is converted to a string... you can also override the __repr__
method if you need to as well
class Cart:
def __init__(self, request):
self.session = request.session
cart = self.session.get('cart')
if not cart :
cart = self.session['cart'] = {} #cria a sessão cart
self.product = cart
def __str__(self):
return f"<CartObject {self.product}>"
...
print(cart)
CodePudding user response:
You can define new method for return your self.product
For example
def get_product(self):
return self.product
or you can just override str function:
def __str__(self):
return f"{self.product}"