title: Django: Cart matching query does not exist
body: <p>I've a form that creates a Cart with the text "Random" in a character field, if there is not a Cart object created. This is only to get this recently object's id if it is not already created.</p>
  
  <pre><code>cart = Cart.objects.get(id=cart_id) 
  </code></pre>
  
  <p>I get an error saying that this query generates an error, however I can see the value of <code>cart_id</code> as a cookie so the Query should execute without problem. But it doesn't according to the error message.</p>
  
  <blockquote>
    <p>Exception Type:   DoesNotExist<br>
    Exception Value:   Cart matching query does not exist.</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/n6mSm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n6mSm.png" alt="enter image description here"></a></p>
  
  <p>As you can see in my view, I'm using this:</p>
  
  <pre><code>    cart_id = self.request.COOKIES.get('cart_id')
      if not cart_id:
          cart = Cart.objects.create(cart_id="Random")
          cart_id = cart.id
          cart = Cart.objects.get(id=cart_id)
  </code></pre>
  
  <p>To get the cookie <code>cart_id</code> if it does not exist I created a <code>Cart object</code> with the Random text, only to get it's ID.</p>
  
  <p><strong>Why I'm getting the error?</strong></p>
  
  <p><strong>View.py</strong>:</p>
  
  <pre><code>class StepOneView(FormView):
      form_class = StepOneForm
      template_name = 'shop/medidas-cantidades.html'
      success_url = 'subir-arte'
  
      def get_initial(self):
          # pre-populate form if someone goes back and forth between forms
          initial = super(StepOneView, self).get_initial()
          initial['size'] = self.request.session.get('size', None)
          initial['quantity'] = self.request.session.get('quantity', None)
          initial['product'] = Product.objects.get(
              category__slug=self.kwargs['c_slug'],
              slug=self.kwargs['product_slug']
          )
  
          return initial
  
      def get_context_data(self, **kwargs):
          context = super().get_context_data(**kwargs)
          context['product'] = Product.objects.get(
              category__slug=self.kwargs['c_slug'],
              slug=self.kwargs['product_slug']
          )
          return context
  
      def form_invalid(self, form):
          print('Step one: form is NOT valid')
  
      def form_valid(self, form):
          cart_id = self.request.COOKIES.get('cart_id')
          if not cart_id:
              cart = Cart.objects.create(cart_id="Random")
              cart_id = cart.id
          cart = Cart.objects.get(id=cart_id)
          item = CartItem.objects.create(
              size=form.cleaned_data.get('size'),
              quantity=form.cleaned_data.get('quantity'),
              product=Product.objects.get(
                  category__slug=self.kwargs['c_slug'],
                  slug=self.kwargs['product_slug']
              ),
              cart=cart
          )
  
          response = HttpResponseRedirect(self.get_success_url())
          response.set_cookie("cart_id", cart_id)
          response.set_cookie("item_id", item.id)
          return response
  </code></pre>
  
  <p><strong>models.py</strong>:</p>
  
  <pre><code>class Cart(models.Model):
      cart_id = models.CharField(max_length=100)
      date_added = models.DateField(auto_now_add=True)
  
      class Meta:
          db_table = 'Cart'
          ordering = ['date_added']
  
      def __str__(self):
          return str(self.id)
  </code></pre>