繁体   English   中英

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

[英]exceptions must be old-style classes or derived from BaseException, not HttpResponseRedirect

这个错误是什么意思? 我正在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"))

我不明白错误的含义,有人可以向我解释我做错了什么吗?

在这一行:

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

回溯告诉你,你不能raise HttpResponseRedirect(...)因为这不是一个Exception已经从继承BaseException

您可能想要做的是在这里使用return或替代地加一个404?

HttpResponseRedirect不是一个例外,而是一个返回重定向的django函数。

由于可能会造成混淆,因此您可能需要使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM