Monday, April 27, 2015

Re: form_invalid redirection with CBVs

Got this working. See below:

class CreateComment(MultipleObjectMixin, FormView):
template_name = 'comment/comment_list.html'
form_class = CommentForm
model = Comment
paginate_by = 5

def post(self, request, *args, **kwargs):
if not request.user.is_authenticated():
return HttpResponseForbidden()
self.object_list = Comment.objects.filter(
object_id=self.kwargs['pk'], content_type_id=self.kwargs['ct']
form = self.get_form()
if form.is_valid():
return self.form_valid(form)

return super(CreateComment, self).post(request, *args, **kwargs)

def get_success_url(self):
return reverse('comment:Comments', kwargs={'ct': self.kwargs['ct'], 'pk': self.kwargs['pk']})

def form_valid(self, form):
data =
data.creator = self.request.user
data.object_id = self.kwargs['pk']
data.content_type_id = self.kwargs['ct']
return super(CreateComment, self).form_valid(form)

def get_context_data(self, **kwargs):
context = super(CreateComment, self).get_context_data(**kwargs)
context['paginate_by'] = self.paginate_by
return context

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment