我正在尝试在博客中创建评论系统。这是视图部分。
def post_content(request,post_slug):
post= Post.objects.get(slug=post_slug)
new_comment=None
#get all comments that are currently active to show in the post
comments= post.comments.filter(active=True)
if request.method=='POST':
comment_form= CommentForm(request.POST)
if comment_form.is_valid():
# saving a ModelForm creates an object of the corresponding model.
new_comment= comment_form.save(commit=False)
new_comment.post=post
new_comment.save()
else:
comment_form=CommentForm()
return render(request,'blog/post_content.html',{'post':post,'comments':comments,'comment_form':comment_form})
还没有评论。现在,我不明白的是,当我发布评论,然后重新加载页面时,我会立即看到评论(这是我不应该看到的)。
据我所知,这应该是流程——当页面重新加载(提交注释后),它首先转到视图并检索活动注释。(
哪个应该是空的,因为还没有保存过,是吗?
)只有当满足if条件且表单有效时,才会保存,如下所示。保存后我还没有检索到评论。但是,仍然,
评论
'变量包含我最近所做的注释。
这是怎么回事?这是什么巫术?请有人给我说清楚!!