这个错误是什么意思?我正在导入from 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"))
我不明白这个错误意味着什么,有人可以向我解释我做错了吗?
答案 0 :(得分:4)
在这一行:
except:
raise HttpResponseRedirect(reverse("notifications_all"))
追溯告诉您,您不能raise HttpResponseRedirect(...)
,因为这不是从Exception
继承的BaseException
。
你可能想要做的是在这里使用return
或者替代提高404而不是?
答案 1 :(得分:1)
HttpResponseRedirect不是例外,而是一个返回重定向的django函数。
由于它可能令人困惑,您可能希望改为使用render:
from django.shortcuts import redirect
...
if next is not None:
return redirect(next)
else:
return redirect(reverse("notifications_all"))