from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic.detail import DetailView
from django.views.generic.edit import FormMixin
from .models import Post, Comment
from .forms import CommentForm
class ParticularPost(FormMixin, DetailView):
template_name='blog/post.html'
model = Post
form_class = CommentForm
def get_success_url(self):
return reverse('post_detail', kwargs={'pk': self.object.id})
def get_context_data(self, **kwargs):
context = super(ParticularPost, self).get_context_data(**kwargs)
context['form'] = CommentForm(initial={'post': self.object})
return context
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
form.save()
return super(ParticularPost, self).form_valid(form)