Home:ALL Converter>django unable to saved forms in database

django unable to saved forms in database

Ask Time:2016-03-17T03:48:43         Author:Coeus

Json Formatter

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

Author:Coeus,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/36045610/django-unable-to-saved-forms-in-database
yy