django unable to saved forms in database

Viewed 630

I'm learning django and I'm trying to save the form using POST method and found its working fine, I'M not able to see the saved message in database(form is not submitted)

Models.py

class Post(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField(max_length=10000)
    pub_date = models.DateTimeField(auto_now_add=True)
    slug = models.SlugField(max_length=200, unique=True)

    def __unicode__(self):
        return self.title

    def description_as_list(self):
        return self.description.split('\n')

class Comment(models.Model):
    title = models.ForeignKey(Post)
    comments = models.CharField(max_length=200)

    def __unicode__(self):
        return '%s' % (self.title)

Forms.py

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('title', 'description')

editPostedForm = modelformset_factory(Post, PostForm)

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('comments',)
        exclude = ('title',)

Views.py

def detail(request, id):
    posts = Post.objects.get(id=id)
    comments = posts.comment_set.all()
    forms = CommentForm
    if request.method == 'POST':
        form = CommentForm(request.POST, instance=posts)
        print form
        if form.is_valid():
            form.save(commit=False)
            form.save()
        else:
          print form.errors
    else:
        form = PostForm()

    return render(request, "detail_post.html", {'forms':forms,'posts': posts,'comments':comments})

Why is the post message is not being saved. I got status code 200 in console, also i get the entered data, but the form is not being saved...Any help is much appreciated

1 Answers
Related