社交登录期望通过 django urlpatterns 引导至 questions.html,而不是引导至主页

问题描述 投票:0回答:1

社交登录预计会导致 questions.html

我目前正在使用 django 学习 urlpatterns。

我希望当我点击通过谷歌登录时,我将被重定向到

questions.html
相反,它会引导到主页。

调试步骤:

  1. 我尝试调整 templates/index.html 模板中的超链接以指向 templates/questions.html
<a href="{% provider_login_url 'google' %}?next=/questions.html">Login With Google</a>
  1. 我已经调整了用户应用程序中的 user/urls.py 文件(管理社交登录)以指向 questions.html
#users/urls.py
urlpatterns = [
    path("", views.home), # home page
    path("questions/", views.logintoquestions), # questions page
    [...]
  1. 我已确保视图函数
    logintoquestions
    在users/views.py中定义
#users/views.py
[...]
def logintoquestions(request):
    return redirect("questions.html")

迁移和 urlpatterns 都是最新的

  1. 我尝试将users/urls.py中的views.home更改为views.logintoquestions
#users/urls.py
urlpatterns = [
    path("", views.logintoquestions), # home page
    path("questions/", views.logintoquestions), # questions page
    [...]

页面现在立即重定向到 questions.html,这不是我想要的 - 我特别希望当用户通过谷歌登录时使用它。

  1. 恢复步骤 4,使“”为
    views.home
#users/views.py
def home(request):
    return render(request, "index.html")

这成功地将“”页面渲染为index.html,更接近我想要的。

  1. 我已尝试将后续功能更新为
#users/views.py
def home(request):
    return render(request, "index.html")

def logintoquestions(request):
    return render(request, "questions.html")

现在主屏幕可以工作,oauth 登录也可以工作,它会导致

/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.
  1. 进一步检查,我可以看到单击此导航栏链接确实会导致具有所需视图的所需
    http://127.0.0.1:8000/questions.html
    .html 页面。
<a class="nav-link" href="{% url 'questions' %}">Top Questions</a>

但是,社交身份验证会导致步骤 6 中出现相同的错误消息。

进一步阅读:

“Django URLS 文档。” Django,https://docs.djangoproject.com/en/5.0/topics/http/urls/。访问日期:2023 年 12 月 9 日。

python html django url-pattern
1个回答
0
投票

如步骤 6 中的错误消息所示,questions.html 不是有效的 urlpattern。 urlpatterns 无法识别 .html 等文件名。

  1. 我已更新模板以不使用

    /questions/
    而不是
    /questions.html

     <a href="{% provider_login_url 'google' %}?next=/questions/">Login With Google</a>
    
  2. 我已在 users/urls.py 中的 urlpattern 中添加了一个名称

#users/urls.py
    path("questions/", views.logintoquestions, name='questions'), # added name='questions'
#users/views.py
def logintoquestions(request):
    return redirect("questions.html")
© www.soinside.com 2019 - 2024. All rights reserved.