title: Django: Implementing a Form within a generic DetailView
body: <p>After working through several google search result pages, I am still desperately stuck at the same problem. I am trying to implement a comment field underneath a blog post. I am thankful for any hints and advice! </p>
  
  <p>I am working on a Blog in Django which is set up with a first, generic ListView to display briefly all available blog posts and with a second, generic DetailView to show the specific blog post in more detail. I now want to place an add_comment_field underneath the specific blog post with all other comments shown underneath. It works when the comment form is displayed on a separate page but not on the same page as the DetailView, which is the desired outcome.  </p>
  
  <p>I suspect this has to do with the interplay between views.py and forms.py but I cannot figure out the problem. </p>
  
  <p>Again, thank you so much for your help!</p>
  
  <p>views.py</p>
  
  <pre><code>from django.shortcuts import render, get_object_or_404, redirect
  from .models import Post, Comment
  from .forms import CommentForm
  from django.views.generic.detail import DetailView
  
  class ParticularPost(DetailView):
      template_name='blog/post.html'
      model = Post
  
      def add_comment_to_post(self, pk):
          post = get_object_or_404(Post, pk=pk)
          if self.method == "POST":
              form = CommentForm(self.POST)
              if form.is_valid():
                  comment = form.save(commit=False)
                  comment.post = post
                  comment.save()
                  return redirect('post_detail', pk=post.pk)
          else:
              form = CommentForm()
          return {'form': form}
  </code></pre>
  
  <p>urls.py</p>
  
  <pre><code>from django.conf.urls import url, include
  from django.views.generic import ListView, DetailView
  from .models import Post, Comment
  from .views import ParticularPost
  
  urlpatterns = [
      url(r'^$', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25], template_name="blog/blog.html")),
      url(r'^(?P&lt;pk&gt;\d+)$', ParticularPost.as_view(), name="post_detail"),
  ]
  </code></pre>
  
  <p>post.html</p>
  
  <pre><code>{% extends "personal/header.html" %}
  {% load staticfiles %}
  {% block content %}
  &lt;div class="container-fluid background_design2 "&gt;
      &lt;div class="header_spacing"&gt;&lt;/div&gt;
      &lt;div class="container post_spacing"&gt;
          &lt;div class="row background_design1 blog_post_spacing inline-headers"&gt;
              &lt;h3&gt;&lt;a href="/blog/{{post.id}}"&gt;{{ post.title }}&lt;/a&gt;&lt;/h3&gt;
              &lt;h6&gt; on {{ post.date }}&lt;/h6&gt;
              &lt;div class = "blog_text"&gt;
                  {{ post.body|safe|linebreaks}}
              &lt;/div&gt;
              &lt;br&gt;&lt;br&gt;
          &lt;/div&gt;
          &lt;div&gt;
              &lt;form method="POST" class="post-form"&gt;{% csrf_token %}
          {{ form.as_p }}
          &lt;button type="submit" class="save btn btn-default"&gt;Send&lt;/button&gt;
      &lt;/form&gt;
          &lt;/div&gt;
          &lt;div class=" row post_spacing background_design1 "&gt;
              &lt;hr&gt;
              {% for comment in post.comments.all %}
                  &lt;div class=" col-md-12 comment"&gt;
                      &lt;div class="date"&gt;{{ comment.created_date }}&lt;/div&gt;
                      &lt;strong&gt;{{ comment.author }}&lt;/strong&gt;
                      &lt;p&gt;{{ comment.text|linebreaks }}&lt;/p&gt;
                  &lt;/div&gt;
              {% empty %}
                  &lt;p&gt;No comments here yet :(&lt;/p&gt;
              {% endfor %}
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  {% endblock %}
  </code></pre>
  
  <p>forms.py</p>
  
  <pre><code>from django import forms
  from .models import Comment
  
  class CommentForm(forms.ModelForm):
  
      class Meta:
          model = Comment
          fields = ('author', 'text',)
  </code></pre>
  
  <p>models.py</p>
  
  <pre><code>from django.db import models
  from django.utils import timezone
  
  class Post(models.Model):
      title = models.CharField(max_length=140)
      body = models.TextField()
      date = models.DateTimeField()
  
      def __str__(self):
          return self.title
  
  class Comment(models.Model):
      post = models.ForeignKey('blog.Post', related_name='comments')
      author = models.CharField(max_length=200)
      text = models.TextField()
      created_date = models.DateTimeField(default=timezone.now)
  
      def __str__(self):
          return self.text
  </code></pre>