title: NOT NULL constraint failed:orders_order.user_id
body: <p>I am new to django and i am building an ecommerce website.
  When i try to make a new order in my website i got this error:
  NOT NULL constraint failed:orders_order.user_id</p>
  <p>each user can have multiple orders and i want to show the orders in the users profile but i am having problem with placing orders</p>
  <p>models.py</p>
  <pre><code>class Order(models.Model):
  user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
  address = models.CharField(max_length=250)
  postal_code = models.CharField(max_length=11)
  city = models.CharField(max_length=100)
  created = models.DateTimeField(auto_now_add=True)
  updated = models.DateTimeField(auto_now=True)
  paid = models.BooleanField(default=False)
  
  class Meta:
      ordering = ('-created',)
  
  def __str__(self):
      return f'Order {self.id}'
  
  def get_total_cost(self):
      return sum(item.get_cost() for item in self.items.all())
  
  
  class OrderItem(models.Model):
  order = models.ForeignKey(Order, related_name='items', on_delete=models.CASCADE)
  
  product = models.ForeignKey(Product, related_name='order_items', on_delete=models.CASCADE)
  
  price = models.DecimalField(max_digits=10, decimal_places=2)
  quantity = models.PositiveIntegerField(default=1)
  
  def __str__(self):
      return str(self.id)
  
  def get_cost(self):
      return self.price * self.quantity
  </code></pre>
  <p>forms.py</p>
  <pre><code>class OrderCreateForm(forms.ModelForm):
  class Meta:
      model = Order
      fields = ['address', 'postal_code', 'city']
  </code></pre>
  <p>views.py</p>
  <pre><code>def order_create(request):
  cart = Cart(request)
  if request.method == 'POST':
      user = request.user
      form = OrderCreateForm(request.POST)
      if form.is_valid():
          order = form.save()
          for item in cart:
              OrderItem.objects.create(order=order, product=item['product'], price=item['price'],
                                       quantity=item['quantity'])
          context = {
              'order': order,
          }
          # clear the cart
          cart.clear()
          return render(request, 'order/created.html', context)
  else:
      form = OrderCreateForm()
  context = {'cart': cart,
             'form': form,
             }
  return render(request, 'order/create.html', context)
  </code></pre>
  <p>template</p>
  <pre><code>{% extends 'shared/_MainLayout.html' %}
  
  {% block content %}
  &lt;h1&gt;Checkout&lt;/h1&gt;
  
  &lt;div class=&quot;order-info&quot;&gt;
      &lt;h3&gt;Your order&lt;/h3&gt;
      &lt;ul&gt;
          {% for item in cart %}
              &lt;li&gt;
                  {{ item.quantity }}x {{ item.product.name }}
                  &lt;span&gt;${{ item.total_price }}&lt;/span&gt;
              &lt;/li&gt;
          {% endfor %}
      &lt;/ul&gt;
      &lt;p&gt;Total: ${{ cart.get_total_price }}&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;form method=&quot;post&quot; class=&quot;order-form&quot;&gt;
      {{ form.as_p }}
      &lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Place order&quot;&gt;&lt;/p&gt;
      {% csrf_token %}
  &lt;/form&gt;
  {% endblock %}
  </code></pre>