[英]Getting 'ERR_TOO_MANY_REDIRECTS' error when using custom Middleware in Django
[英]ERR_TOO_MANY_REDIRECTS in redirect with middleware in Django
我編寫了這個程序,以便在滿足某些條件時將用戶移動到我的目標頁面。
這是我的自定義 Django middleware
:
def check_userprofile_middleware(get_response):
def middleware(request):
response = get_response(request)
if request.user.is_authenticated:
# Profile conditions goes here.
if profile_condition:
return redirect(reverse('base:edit_user_profile'))
return response
return middleware
如果我在if
語句中使用return
,它會重定向到 'base:edit_user_profile' url,但之后我在瀏覽器上看到此錯誤:
This page isn’t working
127.0.0.1 redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS
如果我不在if
語句中使用return
,一切都會正確,除了重定向!
這有什么問題?
通過@gelonida 的提示,我收到了答案。 我確實添加了這一行,問題得到解決:
while not (request.path == reverse('base:edit_edupanel_user_profile')):
return redirect(reverse('base:edit_edupanel_user_profile'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.