title: Django Set & Retrieve Session Data
body: <p>Im working on an eCommerce website using Django. Currently working on the cart app where im using session in django for the first time. Im having problems with setting and retrieving session data which is empty when i print it.</p>
  
  <p>PS: the data from the session will be used to retrieve the cart for the user or update the cart if the cart was created as a visitor.</p>
  
  <p>Not being able to retrieve session data result in a new session being created every time i refresh the browser.</p>
  
  <p>SETTINGS.PY</p>
  
  <pre><code>INSTALLED_APPS = [
      'django.contrib.admin',
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.messages',
      'django.contrib.staticfiles',
  
      # user apps
      'carts',
      'products',
      'search',
      'tags',
  
      # 3rd class app
      'bootstrap4',
  ]
  
  MIDDLEWARE = [
      'django.middleware.security.SecurityMiddleware',
      'django.contrib.sessions.middleware.SessionMiddleware',
      'django.middleware.common.CommonMiddleware',
      'django.middleware.csrf.CsrfViewMiddleware',
      'django.contrib.auth.middleware.AuthenticationMiddleware',
      'django.contrib.messages.middleware.MessageMiddleware',
      'django.middleware.clickjacking.XFrameOptionsMiddleware',
  ]
  </code></pre>
  
  <p>CART APP</p>
  
  <p>MODELS.PY</p>
  
  <pre><code>from django.conf import settings
  from django.db import models
  
  from products.models import Product
  
  User = settings.AUTH_USER_MODEL
  
  
  class CartManager(models.Manager):
      def new(self, user=None):
          user_obj = None
          if user is not None:
              if user.is_authenticated:
                  user_obj = user
          return self.model.objects.create(user=user_obj)
  
  class Cart(models.Model):
      user        = models.ForeignKey(User, null=True, blank=True, on_delete=models.DO_NOTHING)
      products    = models.ManyToManyField(Product, blank=True)
      total       = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
      timestamp   = models.DateTimeField(auto_now_add=True)
      updated     = models.DateTimeField(auto_now=True)
  
      objects = CartManager()
  
      def __str__(self):
          return str(self.id)
  </code></pre>
  
  <p>VIEWS.PY</p>
  
  <pre><code>from django.shortcuts import render
  
  from .models import Cart
  
  
  def cart_home(request):
      cart_id = request.session.get('cart_id')
      qs = Cart.objects.filter(id=cart_id)
      print(cart_id, qs)
      # print(dir(request.session))
      if qs.count() == 1:
          print('Cart ID exists')
          cart_obj = qs.first()
          if request.user.is_authenticated and cart_obj.user is None:
              cart_obj.user = request.user
              cart_obj.save()
      else:
          cart_obj = Cart.objects.new(user=request.user)
          request.session['card_id']  = cart_obj.id
          # request.session.modified = True
          # print(dir(request.session))
          # print(request.session.get_expiry_age())
      return render(request, 'carts/home.html', {})
  </code></pre>