Hi All,

I have been stuck on this for the last few hours and nothing seems to work. 
I would greatly appreciate your help. 

I created a Comment model (which uses ContentType) so that I can use it 
when any model that I want to. 

I also created a CommentForm() ModelForm instance for the Comment model. 

I am testing it with the BlogPost model for adding comments to it. But when 
I submit the form, it doesn't save it to the DB. How can I fix this?

Here are the models in question:

a. Comment model:
        class Comment(models.Model):
heading = models.CharField(max_length=55, verbose_name="Subject")
body = models.TextField(verbose_name="Comment Here")
comment_pubdate = models.DateTimeField(auto_now_add=True, 
verbose_name="Date Added")
poster = models.ForeignKey(settings.AUTH_USER_MODEL, editable=False)
content_type = models.ForeignKey(ContentType, related_name="comments")
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')


b. The CommentForm
         class CommentForm(ModelForm):
class Meta:
model = Comment
fields = ('heading', 'body')

c. The postcomment() view:

     def postcomment(request, object_id, object_link):
          ct = ContentType.objects.get(id=obj_id)
          post = ct.get_object_for_this_type(link=obj_link)

          if request.method=="POST" and not(request.user.is_anonymous):
              poster = request.user
              my_comment = Comment(content_object=post, poster=poster)
              comment_form = CommentForm(request.POST, instance=my_comment)
              
              if comment_form.is_valid():
                    comment_form.save()
                    return HttpResponseRedirect(post.get_absolute_url())

               else:
                      # Placeholder for testing if there was an error with 
the form
                    return HttpResponse("Error!")

        else:
               comment_form = CommentForm()

        return HttpResponseRedirect(post.get_absolute_url())


When I render the BlogPost page (from another view), it loads without 
errors. The BlogPost page's template has (in addition to the rendering 
stuff):
 <form action="{% url 'blog:postcomment' post.id post.link '%}" 
method="post">

(and rest of the <form> stuff)

But when I submit the form, it doesn't process it. Where did I go wrong, in 
the view or elsewhere? 
Thank you for your help.

Sincerely,
Muhammad

-- 
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+unsubscr...@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/cc9b6d71-1335-49cc-82c1-8a01b6831e9a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to