0

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
      
  • 1
    you can override the `__str__` and `__repr__` methods of any class you want in python – Joran Beasley Sep 26 '22 at 18:22
  • Does this answer your question? [How do I change the string representation of a Python class?](https://stackoverflow.com/questions/4912852/how-do-i-change-the-string-representation-of-a-python-class) – Code-Apprentice Sep 26 '22 at 18:29

3 Answers3

3

print(request.session.items())

Dinky Arora
  • 106
  • 4
2

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)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
1

You can define new method like get_product- for your Cart class to get your class attribute

For example

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 get_product(self):
        return self.product

And then you can use get_product method after create new object from Cart class:

def cart_view(request):
    cart = Cart(request)
    print(cart.get_product()) # this line where you call your new method 
    if request.user.is_authenticated:
        return render(request, 'cart/cart_page.html')
    else:
        return redirect('account:login')

or you can just override str function:

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"{self.product}"

now you can just print out your cart object, it will print your product

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')
Ali Kocak
  • 70
  • 8
  • the get_product did'nt work – kaynan felipe rodrigues vieira Sep 26 '22 at 18:46
  • 1
    @kaynanfeliperodriguesvieira they mean you would then do `print(cart.get_product())` but you could also just do `print(cart.product)` – Joran Beasley Sep 26 '22 at 18:57
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 30 '22 at 09:23