简体   繁体   English

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

[英]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.

相关问题 异常必须是旧式类或派生自BaseException,而不是NoneType - exceptions must be old-style classes or derived from BaseException, not NoneType TypeError:异常必须是旧式类或从BaseException派生,而不是str - TypeError: exceptions must be old-style classes or derived from BaseException, not str TypeError:exceptions必须是旧式类或从BaseException派生,而不是str - TypeError:exceptions must be old-style classes or derived from BaseException, not str TypeError:异常必须是旧式类或派生自BaseException,而不是MagicMock - TypeError: exceptions must be old-style classes or derived from BaseException, not MagicMock django crash:异常必须是旧式类或派生自BaseException,而不是str - django crash: exceptions must be old-style classes or derived from BaseException, not str TypeError:exception必须是旧式类或派生自BaseException,而不是NoneType - TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType Python - 继承旧式类 - Python - inheriting from old-style classes 异常必须从 BaseException 派生 - Exceptions must derive from BaseException Pytest & 自定义异常:TypeError:异常必须从 BaseException 派生,而不是 class 'module' - Pytest & custom exception : TypeError: exceptions must be derived from BaseException, not class 'module' 具有旧式类的Python描述符 - Python descriptors with old-style classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM