title: DJANGO: Product matching query does not exist
body: <p><a href="https://i.stack.imgur.com/HHh7R.png" rel="nofollow noreferrer">Error message</a></p>
  <p>I don’t understand what’s the matter. I do everything exactly according to the courses, but knocks out an error:
  <strong>Request URL: <a href="http://127.0.0.1:8000/products/basket-add/12/" rel="nofollow noreferrer">http://127.0.0.1:8000/products/basket-add/12/</a> Django Version: 3.2.4 Exception Type: DoesNotExist Exception Value:
  Product matching query does not exist.</strong></p>
  <p>Add object:</p>
  <pre><code>def basket_add(request, product_id):
      current_page = request.META.get('HTTP_REFERER')
      product = Product.objects.get(id=product_id)
      baskets = Basket.objects.filter(user=request.user, product=product)
  
      if not baskets.exists():
          Basket.objects.create(user=request.user, product=product, quantity=1)
          return HttpResponsePermanentRedirect(current_page)
      else:
          basket = baskets.first()
          basket.quantity += 1
          basket.save()
          return HttpResponsePermanentRedirect(current_page)
  </code></pre>
  <p>Delete:</p>
  <pre><code>def basket_delete(request, id):
      basket = Basket.objects.get(id=id)
      basket.delete()
      return HttpResponsePermanentRedirect(request.META.get('HTTP_REFERER'))
  </code></pre>
  <p>template:</p>
  <pre><code>&lt;a href=&quot;{% url 'products:basket_delete' basket.id %}&quot; style=&quot;text-decoration: none; color: gray;&quot;&gt;
     &lt;i class=&quot;far fa-trash-alt&quot;&gt;&lt;/i&gt;
  &lt;/a&gt;
  </code></pre>
  <p>URLs:</p>
  <pre><code>from django.urls import path
  from products.views import products, basket_add, basket_delete
  
  app_name = 'products'
  
  urlpatterns = [
      path('', products, name=&quot;index&quot;),
      path('basket-add/&lt;int:product_id&gt;/', basket_add, name=&quot;basket_add&quot;),
      path('basket-add/&lt;int:id&gt;/', basket_delete, name=&quot;basket_delete&quot;),
  ]
  </code></pre>
  <p>Models:</p>
  <pre><code>from django.db import models
  from users.models import User
  
  class ProductCategory(models.Model):
      name = models.CharField(max_length=64, unique=True)
      description = models.TextField(blank=True)
  
      class Meta:
          verbose_name_plural = &quot;Product Categories&quot;
  
      def __str__(self):
          return self.name
  
  
  class Product(models.Model):
      name = models.CharField(max_length=256)
      image = models.ImageField(upload_to='products_images', blank=True)
      description = models.TextField(blank=True)
      short_description = models.CharField(max_length=64, blank=True)
      price = models.DecimalField(max_digits=8, decimal_places=2, default=0)
      quantity = models.PositiveIntegerField(default=0)
      category = models.ForeignKey(ProductCategory, on_delete=models.CASCADE)
  
      def __str__(self):
          return f&quot;{self.name}  ({self.category.name})&quot;
  
  
  class Basket(models.Model):
      user = models.ForeignKey(User, on_delete=models.CASCADE)
      product = models.ForeignKey(Product, on_delete=models.CASCADE)
      quantity = models.PositiveIntegerField(default=0)
      created_timestamp = models.DateTimeField(auto_now_add=True)
  
      def __str__(self):
          return f&quot;Basket for {self.user.username} | Product {self.product.name}&quot;
  </code></pre>
  <p><a href="https://i.stack.imgur.com/MkAGD.jpg" rel="nofollow noreferrer">Database</a>
  <a href="https://i.stack.imgur.com/y5gv7.jpg" rel="nofollow noreferrer">Database2</a></p>
  <p><strong>On version 3.2.4, Django throws this error, on 2.8 it throws it to another page (and not the current one) and sometimes adds a new item to the cart and never removes it from it. I saw the advice to use get_or_404, but it won't fix the error.</strong></p>