Exemplo n.º 1
0
def add_comment_to_post(request, pk):
    post = get_object_or_404(Item, pk=pk)
    #del_comment = get_object_or_404(Comment, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.item = post
            comment.author = str(request.user)
            #post.comments = comment
            comment.save()
            #post.save()
            return redirect('todo.views.post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'todo/add_comment_to_post.html', {'form': form})
Exemplo n.º 2
0
def comment_edit(request,pk):
    #post = get_object_or_404(Item, pk=pk)
    del_comment = get_object_or_404(Comment, pk=pk)
    #post_pk = del_comment.item.pk
    #del_comment.delete()
    if request.method == "POST":
        form = CommentForm(request.POST, instance=del_comment)
        if form.is_valid():
            comment = form.save(commit=False)
            #comment.item = post
            #post.comments = comment
            comment.author = str(request.user)
            comment.date = datetime.datetime.now()
            post_pk = comment.item.pk
            comment.save()

            #post.save()
            return redirect('todo.views.post_detail', post_pk)
    else:
        form = CommentForm(instance=del_comment)
    #del_comment.delete()
    return render(request, 'todo/add_comment_to_post.html', {'form': form})