gpt4 book ai didi

python - 异常必须是旧式类或派生自 BaseException,而不是 HttpResponseRedirect

转载 作者:行者123 更新时间:2023-11-28 21:48:17 25 4
gpt4 key购买 nike

这个错误是什么意思?我正在从 django.shortcuts import render, Http404, HttpResponseRedirect 导入
但是为什么我不能使用 HttpResponseRedirect?我读了追溯,但那里没有任何用处......这是我的代码

@login_required
def read(request, id):
try:
next = request.GET.get('next', None)
notification = Notification.objects.get(id=id)
if notification.recipient == request.user:
notification.read = True
notification.save()
if next is not None:
return HttpResponseRedirect(next)
else:
return HttpResponseRedirect(reverse("notifications_all"))
else:
raise Http404
except:
raise HttpResponseRedirect(reverse("notifications_all"))

我不明白错误是什么意思,有人可以向我解释一下我做错了什么吗?

最佳答案

在这一行中:

except:
raise HttpResponseRedirect(reverse("notifications_all"))

回溯告诉您不能raise HttpResponseRedirect(...) 因为那不是从BaseException 继承的Exception

您可能想做的是在这里使用 return 或替代地引发 404?

关于python - 异常必须是旧式类或派生自 BaseException,而不是 HttpResponseRedirect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35619135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com