views.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:onreview 作者: ichi404gh 项目源码 文件源码
def add_comment(request, post_id):
    if request.method == 'GET':
        post = Post.objects.get(pk=post_id)
        form = CommentForm({'post_id':post.id, 'code':post.code})
        return render(request, 'add_comment_form.html', {'post': post, 'form':form})
    else:
        form = CommentForm(request.POST or None)
        if form.is_valid():
            Comment.objects.create(
                code=html.escape(form.cleaned_data['code']),
                description=html.escape(form.cleaned_data['description']),
                post=Post.objects.get(pk=form.cleaned_data['post_id']),
                author=request.user
            )
            return redirect('/post/{}'.format(form.cleaned_data['post_id']), permanent=False)

        post = Post.objects.get(pk=form.cleaned_data['post_id'])
        return render(request, 'add_comment_form.html', {'post': post, 'form':form})
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号