diff options
| author | Bobby <[email protected]> | 2022-09-20 21:04:25 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-09-20 21:04:25 -0400 |
| commit | ac0ca39b0891e4f9f53dbea85c1b30b5042252f0 (patch) | |
| tree | a932ee180a099fb838ac5cc96ad64890553db5d8 | |
| parent | 5d55853d7fa8d0f2e9c1ccaa5f0ac71e3333a706 (diff) | |
| download | thatcomputerscientist-ac0ca39b0891e4f9f53dbea85c1b30b5042252f0.tar.xz thatcomputerscientist-ac0ca39b0891e4f9f53dbea85c1b30b5042252f0.zip | |
Ability to delete comments
| -rw-r--r-- | blog/urls.py | 1 | ||||
| -rw-r--r-- | blog/views.py | 14 | ||||
| -rw-r--r-- | templates/blog/post.html | 2 |
3 files changed, 16 insertions, 1 deletions
diff --git a/blog/urls.py b/blog/urls.py index 09161c16..2927837b 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -13,5 +13,6 @@ urlpatterns = [ path('post/<str:slug>', views.post, name='post'), path('post/<str:slug>/comment', views.comment, name='comment'), path('post/<str:slug>/edit_comment', views.edit_comment, name='edit_comment'), + path('post/<str:slug>/delete_comment/<int:comment_id>', views.delete_comment, name='delete_comment'), # path('my/homepage', views.homepage, name='homepage'), ] diff --git a/blog/views.py b/blog/views.py index 6930e6b4..d79a686c 100644 --- a/blog/views.py +++ b/blog/views.py @@ -143,3 +143,17 @@ def edit_comment(request, slug): return redirect('blog:home') else: return redirect('blog:home') + +def delete_comment(request, slug, comment_id): + if request.user.is_authenticated: + try: + comment = Comment.objects.get(id=comment_id) + if comment.user == request.user: + comment.delete() + return redirect('blog:post', slug=slug) + else: + return HttpResponse('Unauthorized!', status=401) + except Comment.DoesNotExist: + return HttpResponse('Comment not found!', status=404) + else: + return redirect('blog:home') diff --git a/templates/blog/post.html b/templates/blog/post.html index 712d7168..fc175183 100644 --- a/templates/blog/post.html +++ b/templates/blog/post.html @@ -35,7 +35,7 @@ • <a href="javascript:;" onclick="editComment({{ comment.id }})">Edit</a> • - <a href="javascript:;">Delete</a> + <a href="{% url 'blog:delete_comment' post.slug comment.id %}" onclick="return confirm('Are you sure you want to delete this comment?')">Delete</a> {% endif %} </p> <p class="comment-body"> |
