Django ModelForm不保存到数据库

时间:2017-04-16 16:34:21

标签: django django-forms

我尝试添加注释,但不会将其保存到数据库中。没有错误。

我的观点是

def add_comment(request, id):
    article = get_object_or_404(Article, id=id)

    if request.method == "POST":
        form = CommentForm(request.POST,instance=article)

        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = article
            comment.save()
            return redirect('articles:detail', pk=article.pk)
    else:
        form = CommentForm()

    template="article/comment.html"
    return render(request, template, {'form': form})

comment.html:

<form method="POST">{% csrf_token %}
{{form.as_p}}
<button type="submit">Send</button>

forms.py

class CommentForm(forms.ModelForm):

class Meta:
    model = Comment
    fields = ('name', 'comment')

0 个答案:

没有答案