[英]exceptions must be old-style classes or derived from BaseException, not HttpResponseRedirect
What does this error mean? 这个错误是什么意思? I'm importing
from django.shortcuts import render, Http404, HttpResponseRedirect
but why can;t I use HttpResponseRedirect? 我正在
from django.shortcuts import render, Http404, HttpResponseRedirect
但为什么不能;我不能使用HttpResponseRedirect? I read traceback, but nothing useful there... here's my code 我读了回溯,但是那里没什么用。。。这是我的代码
@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"))
I don't understand what the error means, can some one explain to me what I did wrong? 我不明白错误的含义,有人可以向我解释我做错了什么吗?
In this line: 在这一行:
except:
raise HttpResponseRedirect(reverse("notifications_all"))
The traceback is telling you that you cannot raise HttpResponseRedirect(...)
because that's not an Exception
that has inherited from BaseException
. 回溯告诉你,你不能
raise HttpResponseRedirect(...)
因为这不是一个Exception
已经从继承BaseException
。
What you probably wanted to do was use return
here instead or alternate raise a 404 instead? 您可能想要做的是在这里使用
return
或替代地加一个404?
HttpResponseRedirect is not an exception, but a django function that returns a redirect. HttpResponseRedirect不是一个例外,而是一个返回重定向的django函数。
Since it can be confusing, you might want to use render instead: 由于可能会造成混淆,因此您可能需要使用render:
from django.shortcuts import redirect
...
if next is not None:
return redirect(next)
else:
return redirect(reverse("notifications_all"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.