title: Django: WARNING - Method Not Allowed (POST)
body: <p>I know what above error means. Seems I can't handle form when it is posted. I can do it in function based views, but in class based views I am tad lost.</p>
  <p>I am creating a comment app and here is <code>forms.py</code> content in <code>comment</code> app:</p>
  <pre><code>class CommentForm(forms.Form):
      content_type = forms.CharField(widget=forms.HiddenInput)
      object_id = forms.CharField(widget=forms.HiddenInput)
      body = forms.CharField(widget=forms.Textarea)
  </code></pre>
  <p>then in <code>DetailView</code> of the blog app, I handled it this way:</p>
  <pre><code>class BlogDetail(DetailView):
      model = Blog
      template_name = 'blogs/blog_detail.html'
      context_object_name = 'blog'
  
      def get_object(self):
          blog_slug = self.kwargs.get('blog_slug')
          return get_object_or_404(Blog, slug=blog_slug)
  
      def get_context_data(self, *args, **kwargs):
          obj = self.get_object()
          context = super().get_context_data(**kwargs)
          context['comments'] = Comment.objects.filter_by_instance(obj)
  
          &quot;&quot;&quot; comment form &quot;&quot;&quot;
          initial_data = {
              'content_type': obj.get_content_type,
              'object_id': obj.id
          }
          if self.request.method == 'POST':
              form = CommentForm(self.request.POST, initial=initial_data)
              if form.is_valid():
                  c_type = form.cleaned_data.get('content_type')
                  content_type = ContentType.objects.get(model=c_type)
                  object_id = form.cleaned_data.get('object_id')
                  body = form.cleaned_data.get('body')
                  new_comment, created = Comment.objects.get_or_create(
                      user=self.request.user,
                      content_type=content_type,
                      object_id=object_id,
                      body=body
                  )
          else: 
              form = CommentForm(initial=initial_data)
  
          context['comment_form'] = form
  
          return context
  </code></pre>
  <p>albeit I passsed <code>form = CommentForm(self.request.POST, initial=initial_data)</code> but there sounds something is going wrong, Can anyone help? Thank you</p>
  <p>edited:</p>
  <pre><code>class BlogDetail(DetailView, FormView):
      model = Blog
      template_name = 'blogs/blog_detail.html'
      context_object_name = 'blog'
      form_class = CommentForm
  
      def get_object(self):
          blog_slug = self.kwargs.get('blog_slug')
          return get_object_or_404(Blog, slug=blog_slug)
  
      def get_initial(self):
          obj = self.get_object()
          return {
              'content_type': obj.get_content_type,
              'object_id': obj.id
          }
  
      def form_valid(self, form):
          c_type = form.cleaned_data.get('content_type')
          content_type = ContentType.objects.get(model=c_type)
          object_id = form.cleaned_data.get('object_id')
          body = form.cleaned_data.get('body')
  
          Comment.objects.create(
              user=self.request.user,
              content_type=content_type,
              object_id=object_id,
              body=body
          )
  </code></pre>
  <p>edit 2:</p>
  <p>Can anyone spot the error with this approach:</p>
  <pre><code>class BlogDetail(DetailView):
      model = Blog
      template_name = 'blogs/blog_detail.html'
      context_object_name = 'blog'
      form_class = CommentForm
  
      def get_object(self):
          blog_slug = self.kwargs.get('blog_slug')
          return get_object_or_404(Blog, slug=blog_slug)
  
      def get(self, request, *args, **kwargs):
          obj = self.get_object()
  
          initial_data = {
              'content_type': obj.get_content_type,
              'object_id': obj.id
          }
          print(&quot;aaaaaaaaaaaaaaaaaaaaaaaaaaaaa&quot;, obj.get_content_type)
          form = self.form_class(initial=initial_data)
  
          return render(request, self.template_name, {'form': form})
  
      def post(self, request, *args, **kwargs):
          form = self.form_class(request.POST)
  
          if form.is_valid():
              c_type = form.cleaned_data.get('content_type')
              content_type_2 = ContentType.objects.get(model=c_type)
              object_id = form.cleaned_data.get('object_id')
              body = form.cleaned_data.get('body')
  
              Comment.objects.create(
                  user=request.user,
                  content_type=content_type_2,
                  object_id=object_id,
                  body=body,
              )
  
          return render(request, self.template_name, {'form': form})
  </code></pre>