簡體   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