Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django How to see the session data insted of the object reference

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>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
      

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading