python - 在 Django 中使用中间件重定向 ERR_TOO_MANY_REDIRECTS
问题描述
我编写了这个程序,以便在满足某些条件时将用户移动到我的目标页面。
这是我的自定义 Djangomiddleware
:
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
如果我使用return
inif
语句,它会重定向到 '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
如果我不使用return
inif
语句,一切都会正确,除了重定向!
这有什么问题?
解决方案
通过@gelonida 的提示,我收到了答案。我确实添加了这一行,问题得到解决:
while not (request.path == reverse('base:edit_edupanel_user_profile')):
return redirect(reverse('base:edit_edupanel_user_profile'))
推荐阅读
- amazon-web-services - aws fargate 在任务定义中定义日志导致云形成永远不会完成
- python - 使用 Rabbitmq 作为经纪人,芹菜工人是在推 api 还是拉 api 上工作?
- maven-surefire-plugin - 为 DynamicTest 设置测试名称
- java - 使用 POST 请求从 Wordpress 网站发送邮件
- excel - VBA 宏删除包含 #N/A 的单元格并向上移动单元格(不是行)
- python - Switching between screens defined in separate .kv (Kivy) files
- ios - Admob 横幅广告最佳做法
- java - Eclipse 未显示 jdk10 的 javadocs
- r - 在 Shiny 应用程序中使用 summarytools::descr() 和 by() 时变量名消失
- c - fseek/fsetpos may discard stream buffer?