title: NOT NULL constraint failed: posts_comment.post_id django
body: <p>I have the basics of a blogs app (I have been following Corey Schafer's tutorial, part 10, if that matters) and I am trying to post and display comments on the post that other users have wrote that only relate to that post. Similar to that of a comment section on Facebook or YouTube.</p>
  
  <p>Currently I'm displaying all comments in the database on every post because I don't know how to filter the results to only show comments for the specific post and I also don't know how to post comments to a specific post, that's where I get the error. </p>
  
  <p>My code is below:</p>
  
  <p>Models.py</p>
  
  <pre><code>class Post(models.Model):
      title = models.CharField(max_length=200)
      content = models.TextField()
      user = models.ForeignKey(User, on_delete=models.CASCADE)
      date_posted = models.DateTimeField(default=timezone.now)
      product = models.ForeignKey(Product, on_delete=models.CASCADE)
  
      def __str__(self):
          return self.title
  
      def get_absolute_url(self):
          return reverse('core:post-detail', kwargs={'pk': self.pk})
  
  class Comment(models.Model):
      user = models.ForeignKey(User, on_delete=models.CASCADE)
      post = models.ForeignKey(Post, on_delete=models.CASCADE)
      content = models.TextField()
      date_posted = models.DateTimeField(default=timezone.now)
  
      def __str__(self):
          return self.user.username 
  </code></pre>
  
  <p>Views.py</p>
  
  <pre><code>class PostDetailView(DetailView):
      model = Post
      context_object_name = 'posts'
  
      def get_context_data(self, **kwargs):
          context = super(PostDetailView, self).get_context_data(**kwargs)
          context['comment'] = Comment.objects.all()
          return context
  
  class CommentCreateView(LoginRequiredMixin, CreateView):
      model = Comment
      fields = ['content']
  
      def form_valid(self, form):
          form.instance.user = self.request.user
          return super().form_valid(form)
  </code></pre>
  
  <p>urls.py (This is the urls.py file in my main app where I'm calling the blogs app views)</p>
  
  <pre><code>path('posts/&lt;int:pk&gt;/', PostDetailView.as_view(), name='post-detail'),
  path('posts/&lt;int:pk&gt;/comment', CommentCreateView.as_view(), name='comment-create'),
  </code></pre>
  
  <p>I then have this form in a file called <em>comment_form.html</em> that I can connect to when I got to <a href="http://127.0.0.1:8000/posts/5/comment" rel="nofollow noreferrer">http://127.0.0.1:8000/posts/5/comment</a> but when I click reply I get 
  <strong>IntegrityError at /posts/5/comment NOT NULL constraint failed: posts_comment.post_id</strong></p>
  
  <pre><code>&lt;form method="POST"&gt;
    {% csrf_token %}
    &lt;fieldset class="form-group"&gt;
      &lt;legend class="border-bottom mb-4"&gt; Reply to Post &lt;/legend&gt;
      {{ form|crispy}}
    &lt;/fieldset&gt;
    &lt;div class="form-group"&gt;. 
      &lt;button type="submit" class="btn btn-success"&gt;Reply&lt;/button&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>If someone could throw their eye over it and let me know if you can find anything that I could do to get it to work, I would greatly appreciate it. Let me know if you need more information/code.</p>
  
  <p>Thanks</p>