我尝试添加注释,但不会将其保存到数据库中。没有错误。
我的观点是
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')