From 564a25b5b1dce6ce317f13bf01afbbfb743bef89 Mon Sep 17 00:00:00 2001 From: taoky Date: Mon, 28 Oct 2024 15:01:26 +0800 Subject: [PATCH] Redirect to challenge when submitted successfully --- frontend/views.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/frontend/views.py b/frontend/views.py index 2edb7cb..1f19c90 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -7,6 +7,7 @@ from django.contrib.admin import site from django.contrib.auth import logout from django.http import Http404, JsonResponse +from django.urls import reverse from django.shortcuts import redirect from django.template.response import TemplateResponse from django.views import View @@ -247,6 +248,10 @@ def check(self, challenge_id): except Error as e: return None + def redirect_with_challenge_hash(self, challenge_name): + url = reverse("hub") + "#" + quote(challenge_name) + return redirect(url) + def check_frequency(self, challenge_id): request = self.request matched_feedbacks = UnidirectionalFeedback.objects.filter(challenge_id=challenge_id, user=request.user) @@ -294,7 +299,7 @@ def post(self, request, challenge_id): too_frequent, latest_feedback = self.check_frequency(challenge_id) if too_frequent: messages.error(request, "提交反馈太过频繁。") - return redirect('hub') + return self.return_template(challenge_name, too_frequent, latest_feedback) contents = request.POST.get("contents") if len(contents) > 1024: messages.error(request, "提交内容超过字数限制。") @@ -319,12 +324,12 @@ def post(self, request, challenge_id): except (requests.exceptions.RequestException, requests.exceptions.HTTPError) as e: messages.error(request, "反馈发送失败,请向管理员反馈此问题。") logger.exception("反馈发送失败") - return self.return_template(challenge_name, too_frequent, latest) + return self.return_template(challenge_name, too_frequent, latest_feedback) feedback = UnidirectionalFeedback.objects.create(challenge_id=challenge_id, user=request.user, contents=contents) feedback.save() messages.success(request, "反馈提交成功。") - return redirect('hub') + return self.redirect_with_challenge_hash(challenge_name) class ScoreView(View):