我目前正在使用 django 学习 urlpatterns。
我希望当我点击通过谷歌登录时,我将被重定向到
questions.html
相反,它会引导到主页。
<a href="{% provider_login_url 'google' %}?next=/questions.html">Login With Google</a>
#users/urls.py
urlpatterns = [
path("", views.home), # home page
path("questions/", views.logintoquestions), # questions page
[...]
logintoquestions
在users/views.py中定义#users/views.py
[...]
def logintoquestions(request):
return redirect("questions.html")
迁移和 urlpatterns 都是最新的
#users/urls.py
urlpatterns = [
path("", views.logintoquestions), # home page
path("questions/", views.logintoquestions), # questions page
[...]
页面现在立即重定向到 questions.html,这不是我想要的 - 我特别希望当用户通过谷歌登录时使用它。
views.home
#users/views.py
def home(request):
return render(request, "index.html")
这成功地将“”页面渲染为index.html,更接近我想要的。
#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.
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 日。
如步骤 6 中的错误消息所示,questions.html 不是有效的 urlpattern。 urlpatterns 无法识别 .html 等文件名。
我已更新模板以不使用
/questions/
而不是 /questions.html
。
<a href="{% provider_login_url 'google' %}?next=/questions/">Login With Google</a>
我已在 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")