Tuesday, July 28, 2015

Re: Using FormMixin with DetailView

Hello James,

Following your advice I turned to using CreateView, and now all the functionality I wanted seems to work fully. As you suggested there is no need for a form.py using this method. When I got more time I might investigate further how to make the original approach work for future reference.  This is the view code:

class PostDetailView(BlogMixin,CreateView):
   
""" A view for displaying a single post """
    template_name
= 'post.html'
    model
= Comment
    fields
= ['body','author_name']
   
def get_context_data(self, **kwargs):
        context
= super(PostDetailView, self).get_context_data(**kwargs)
        context
['post'] =  Post.objects.get(pk=self.kwargs['pk'])
       
return context
   
   
def form_valid(self, form):
        obj
= form.save(commit=False)
        obj
.parent_post = Post.objects.get(pk=self.kwargs['pk'])
        obj
.save()

       return redirect('post-detail', self.kwargs['pk'])


--
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/73949be8-34ba-481a-94a7-9c93004fcb31%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment