Django谷歌社交登录后无法正确重定向至questions页面的问题排查求助
Django谷歌社交登录后无法正确重定向至questions页面的问题排查求助
我最近在学习Django的urlpatterns配置,遇到了一个谷歌社交登录重定向的问题:我期望用户点击“Login With Google”完成登录后,能被重定向到questions.html页面,但实际情况是要么跳转到首页,要么跳转后出现URL匹配错误。以下是我已经尝试过的调试步骤和遇到的具体问题:
已尝试的调试步骤:
- 修改
templates/index.html中的登录链接,添加next参数指向目标页面:<a href="{% provider_login_url 'google' %}?next=/questions.html">Login With Google</a> - 在用户应用的
users/urls.py中配置questions页面的路由:# users/urls.py urlpatterns = [ path("", views.home), # 首页 path("questions/", views.logintoquestions), # questions页面 # ...其他路由 ] - 确保
users/views.py中定义了对应的视图函数:# users/views.py def logintoquestions(request): return redirect("questions.html") - 曾尝试将首页路由直接指向
logintoquestions,结果网站打开就直接跳转到questions页面,这不符合我的需求(我只希望谷歌登录后才跳转至此),所以又改回了原配置:# users/urls.py urlpatterns = [ path("", views.home), # 恢复首页配置 path("questions/", views.logintoquestions), # ...其他路由 ] - 调整视图函数,将
logintoquestions改为直接渲染模板而非重定向:
现在首页能正常显示,谷歌登录流程也能完成,但登录后跳转时出现以下错误:# users/views.py def home(request): return render(request, "index.html") def logintoquestions(request): return render(request, "questions.html")Using the URLconf defined in classroommatrix.urls, Django tried these URL patterns, in this order:
admin/
index/ [name='index']
questions/ [name='questions']
slug:slug/ [name='post_detail']
like/slug:slug [name='post_like']
summernote/
accounts/
questions/
accounts/
logout
The current path, questions.html, didn’t match any of these. - 另外我发现导航栏中的链接
{% url 'questions' %}可以正常跳转到http://127.0.0.1:8000/questions并显示正确页面,但社交登录后的跳转始终报错。
我已经查阅了Django的URL相关文档,但还是没找到问题所在,希望能得到大家的帮助,告诉我应该怎么调整配置,才能让谷歌社交登录后正确重定向到questions页面?
备注:内容来源于stack exchange,提问作者Laurie Crean
相关产品推荐
相关产品推荐

