Tuesday, July 28, 2015

Re: Using FormMixin with DetailView

Thanks your your response. I have now gone past that problem and the form renders, although on submission of the form nothing happens. The 'POST -----' string is printed, which means I am on the right track, but no new object is created. I tried to simplify the comment form (remove the FK and the method override) in case there was a problem with that code, but I still dont get a comment created. Here is the current state of the code:

class CommentForm(forms.ModelForm):
   
class Meta:
        model
= Comment
        exclude
= ("parent_post","created_at")


   
def save():
       
print "IN VIEW"
       
if request.method == "POST":
            parent_fk
= self.object
           
print "PARRENT"
           
print parent_fk
            form
= CommentForm(request.POST)
           
if form.is_valid():
                new_comment
= form.save(commit=False)
                new_comment
.parent_post = parent_fk
                new_comment
.save()
               
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))


class PostView(DetailView):
   
""" A view for displaying a single post """
    template_name
= 'post.html'
    model
= Post
   
def get_context_data(self, **kwargs):
        context
= super(PostView, self).get_context_data(**kwargs)
        context
['form'] = CommentForm()
       
return context
   
class PostDetailView(View):


   
def get(self, request, *args, **kwargs):
        view
= PostView.as_view()
       
return view(request, *args, **kwargs)


   
def post(self, request, *args, **kwargs):
        view
= PostComment.as_view()
       
return view(request, *args, **kwargs)


class PostComment(  SingleObjectMixin , FormView):
    template_name
= 'post.html'
    form_class
= CommentForm
    model
= Post


   
def post(self, request, *args, **kwargs):
       
print "POST ----"
       
self.object = self.get_object()
       
return super(PostComment, self).post(request, *args, **kwargs)


   
def get_success_url(self):
       
return reverse('post-detail', kwargs={'pk': self.object.pk})




    url
(r'^post/(?P<pk>[\d]+)/$', views.PostDetailView.as_view(), name="post-detail"),



--
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/6af1087a-a95e-4eb8-ac5b-68823a45b4ec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment