title: Not able to fetch product id in ecommerce application -django
body: <p><strong>views.py</strong></p>
  <pre><code>    from django.shortcuts import render,redirect
      from .models import Cart
      from new_app.models import Product
      def cart_home(request):
                          
          cart_obj,new_obj=Cart.objects.new_or_get(request)
          products=Cart.objects.all()
          return render(request,'carts/home.html',{})
                      
                      
      def cart_update(request):
          print(request.POST)
          product_id=1
          print('id below')
          print(product_id) // not able to get the value of product id in console
          product_obj=Product.objects.get(id=product_id)
          cart_obj,new_obj=Cart.objects.new_or_get(request)
          if product_obj in cart_obj.products.all():
                cart_obj.products.remove(product_obj)
          else:
                cart_obj.products.add(product_obj)
          return redirect('home')
  
  </code></pre>
  <p><strong>models.py</strong>(cart)</p>
  <pre><code>
      from django.db import models
      from django.conf import settings
      from new_app.models import Product
      from django.db.models.signals import pre_save,post_save,m2m_changed
      
      
      
      User=settings.AUTH_USER_MODEL
      
      class CartManager(models.Manager):
          def new_or_get(self,request):
              cart_id=request.session.get(&quot;cart_id&quot;,None)
              # qs=self.get_queryset().filter(id=cart_id)
              qs=self.get_queryset().only('products')
      
              print(qs)
              if qs.count()==1:
                      new_obj=False
                      cart_obj=qs.first()
                      print('cart obj below')
                      print(cart_obj)
                      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_cart(user=request.user)
                      new_obj=True
                      request.session['cart_id']=cart_obj.id
              return cart_obj,new_obj
      
          def new_cart(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.CASCADE)
          products=models.ManyToManyField(Product,blank=True)
          subtotal=models.DecimalField(default=0.00,max_digits=100,decimal_places=2)
      
          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)
      
      
      def m2m_changed_cart_receiver(sender,instance,action,*args,**kwargs):
          print(action)
          if action=='post_add' or action=='post_remove' or action=='clear':
              products=instance.products.all()
              total=0
              for x in products:
                  total += x.price
              if instance.subtotal != total:
                  instance.subtotal=total
                  instance.save()
      m2m_changed.connect(m2m_changed_cart_receiver,sender=Cart.products.through)
      
      
      def pre_save_cart_receiver(sender,instance,*args,**kwargs):
          if instance.subtotal&gt;0:
              instance.total=instance.subtotal + 10
          else:
              instance.total=0.00
      
      pre_save.connect(pre_save_cart_receiver,sender=Cart)
  </code></pre>
  <p><strong>update_cart.html</strong></p>
  <pre><code>    &lt;form method='POST' action='{% url 'update' %}' class=&quot;form &quot;&gt;{% csrf_token %}
        &lt;input type=&quot;hidden&quot;  name='product_id' value= &quot;{{ product_id }}&quot;&gt;
        {% if product in cart.products.all %}
          &lt;button type=&quot;submit&quot; class=&quot;btn btn-link&quot;&gt;remove&lt;/button&gt;
        {% else %}
          &lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;add to cart&lt;/button&gt;
        {% endif %}
        &lt;/form&gt;
  
  </code></pre>
  <p><strong>output in terminal:</strong></p>
  <pre><code>    &lt;QueryDict: {'csrfmiddlewaretoken': ['FMk2gTq6XXxZ2HU40I6h4b3WtPl59Drf1urwUNufDZUeSFPMzGNwU4L1QuGCiCbB'], 'product_id':
       ['']}&gt;     -------- GETTING EMPTY DICTIONARY  INSTEAD OF GETTING PRODUCT ID i.e. 1
      id below
      1
      &lt;QuerySet [&lt;Cart: 13&gt;]&gt;
  </code></pre>
  <p>So, I am unable to fetch the product id there besides the csrf....
  I tried to individually print the product id.. which came as 1...(written under&quot;id below&quot; in output)</p>
  <ol>
  <li>I am facing an error while working on the CART component of an ecommerce website. The issue is tat the product_id (which has a foreign key relationship with the PRODUCT model) is not getting fetched from the views.py file (in cart). I have imported the PRODUCT model. But, I am getting an empty dictionary when I am trying to print the product_id.</li>
  <li>As you can see, I have copied the output from the terminal, the csrf token , there is a dictionary for product_id , that is empty. no values is being shown there.
  3.The product id is getting printed if i print it from the views.py from PRODUCT app.
  4.The products are getting added to the cart, as I checked that in admin.</li>
  </ol>