简体   繁体   中英

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? 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 .

What you probably wanted to do was use return here instead or alternate raise a 404 instead?

HttpResponseRedirect is not an exception, but a django function that returns a redirect.

Since it can be confusing, you might want to use render instead:

from django.shortcuts import redirect

...

     if next is not None:
       return redirect(next)
     else:
       return redirect(reverse("notifications_all"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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