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']
).order_by('-created')
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 = form.save(commit=False)
data.creator = self.request.user
data.object_id = self.kwargs['pk']
data.content_type_id = self.kwargs['ct']
data.save()
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/be9e0c75-3da1-4b67-bee7-bc98e0cd13da%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment