From 1c4df10eda2003af5fbb26d694cd933f79d35924 Mon Sep 17 00:00:00 2001 From: xiokuai Date: Fri, 20 Feb 2026 22:59:03 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E5=88=A0=E9=99=A4=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- forum/templates/forum/post_detail.html | 12 +++++++++++- forum/urls.py | 1 + forum/views.py | 10 +++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/forum/templates/forum/post_detail.html b/forum/templates/forum/post_detail.html index 71fa666..a0af886 100644 --- a/forum/templates/forum/post_detail.html +++ b/forum/templates/forum/post_detail.html @@ -43,7 +43,17 @@

#{{ forloop.counter }} {{ comment.author.username }} - {{ comment.created_at|date:"Y-m-d H:i" }} + + {{ comment.created_at|date:"Y-m-d H:i" }} + {% if comment.author == request.user %} +
+ {% csrf_token %} + +
+ {% endif %} +
{{ comment.content_html | safe }} diff --git a/forum/urls.py b/forum/urls.py index b87f495..0a64d34 100644 --- a/forum/urls.py +++ b/forum/urls.py @@ -8,6 +8,7 @@ path('posts/create/', views.post_create, name='post_create'), path('posts//', views.PostDetailView.as_view(), name='post_detail'), path('posts//delete/', views.PostDeleteView.as_view(), name='post_delete'), + path('comments//delete/', views.comment_delete_view, name='comment_delete'), path('login/', views.LoginView.as_view(), name='login'), path('settings/', views.user_settings_view, name='settings'), path('settings/user_delete/', views.user_delete_view, name='user_delete'), diff --git a/forum/views.py b/forum/views.py index 1a06458..d83e726 100644 --- a/forum/views.py +++ b/forum/views.py @@ -11,7 +11,7 @@ from .utils import send_group_notification from forum.form import MDEditorCommentForm, MDEditorModelForm -from forum.models import Item, Post, Rating +from forum.models import Comment, Item, Post, Rating from forum.bots_manager import manager # Create your views here. @@ -153,6 +153,14 @@ def get_queryset(self): qs = super().get_queryset() return qs.filter(author=self.request.user) +@login_required +def comment_delete_view(request, comment_id): + comment = get_object_or_404(Comment, id=comment_id, author=request.user) + post_id = comment.post.id + if request.method == 'POST': + comment.delete() + return redirect('post_detail', post_id=post_id) + @login_required def user_settings_view(request): webpush = {"group": "webpush_new_posts" } From 4e768edfc6089502ac7b1269e86b1a3ce8a9d3e2 Mon Sep 17 00:00:00 2001 From: xiokuai Date: Mon, 23 Feb 2026 13:02:47 +0800 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=E8=AF=84=E8=AE=BA=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=94=B9=E4=B8=BA=E7=8B=AC=E7=AB=8B=E7=A1=AE=E8=AE=A4?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用随机加法验证替代简单弹窗,风格与帖子删除页面统一。 解决分支冲突,合并楼层号功能。 --- .../templates/forum/comment_check_delete.html | 37 +++++++++++++++++++ forum/templates/forum/post_detail.html | 9 ++--- forum/views.py | 15 ++++++-- 3 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 forum/templates/forum/comment_check_delete.html diff --git a/forum/templates/forum/comment_check_delete.html b/forum/templates/forum/comment_check_delete.html new file mode 100644 index 0000000..42bc55f --- /dev/null +++ b/forum/templates/forum/comment_check_delete.html @@ -0,0 +1,37 @@ +{% extends 'base.html' %} + +{% block title %}确认删除评论{% endblock %} + +{% block content %} +
+
+
+
+
+
删除评论
+

该评论将会永久消失!(真的很久!)

+

请计算 {{ a }} + {{ b }} = ?

+
+ {% csrf_token %} + + +
+
+ + 取消 +
+
+
+
+
+
+
+ + +{% endblock %} diff --git a/forum/templates/forum/post_detail.html b/forum/templates/forum/post_detail.html index a0af886..b56e98f 100644 --- a/forum/templates/forum/post_detail.html +++ b/forum/templates/forum/post_detail.html @@ -46,12 +46,9 @@

{{ comment.created_at|date:"Y-m-d H:i" }} {% if comment.author == request.user %} -
- {% csrf_token %} - -
+ + + {% endif %}

diff --git a/forum/views.py b/forum/views.py index d83e726..6dc331f 100644 --- a/forum/views.py +++ b/forum/views.py @@ -1,4 +1,4 @@ -import re +import re, random from django.urls import reverse_lazy from django.utils import timezone from django.contrib.auth.models import User @@ -156,10 +156,17 @@ def get_queryset(self): @login_required def comment_delete_view(request, comment_id): comment = get_object_or_404(Comment, id=comment_id, author=request.user) - post_id = comment.post.id if request.method == 'POST': - comment.delete() - return redirect('post_detail', post_id=post_id) + answer = request.POST.get('answer', '') + expected = request.POST.get('expected', '') + if answer == expected: + post_id = comment.post.id + comment.delete() + return redirect('post_detail', post_id=post_id) + a, b = random.randint(1, 9), random.randint(1, 9) + return render(request, 'forum/comment_check_delete.html', { + 'comment': comment, 'a': a, 'b': b, 'answer': a + b, + }) @login_required def user_settings_view(request):