social login expects to lead to questions.html via django urlpatterns, instead leads to home page

17 Views Asked by At

social login expects to lead to questions.html

I am currently learning urlpatterns with django.

I expect that when I click on login via google, I will be redirected to questions.html Instead it leads to the home page.

debugging steps:

  1. I have tried adjusting the hyperlink in the templates/index.html template to point to templates/questions.html
<a href="{% provider_login_url 'google' %}?next=/questions.html">Login With Google</a>
  1. I have adjusted the user/urls.py file in users app (manages social login) to point to questions.html
#users/urls.py
urlpatterns = [
    path("", views.home), # home page
    path("questions/", views.logintoquestions), # questions page
    [...]
  1. I have ensured the view function logintoquestions is defined in users/views.py
#users/views.py
[...]
def logintoquestions(request):
    return redirect("questions.html")

migrations are all up to date and the urlpatterns

  1. I have tried changing views.home to views.logintoquestions in users/urls.py
#users/urls.py
urlpatterns = [
    path("", views.logintoquestions), # home page
    path("questions/", views.logintoquestions), # questions page
    [...]

page now redirects to questions.html immediately which is not what I want - I want specifically it to be used when the user logs in via google.

  1. Reverted step 4 so that "" is views.home
#users/views.py
def home(request):
    return render(request, "index.html")

this successfully renders the "" page as index.html, closer to what I want.

  1. I have tried updating the subsequent function to
#users/views.py
def home(request):
    return render(request, "index.html")

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

now the homescreen works, and oauth login works, and it leads to /questions.html but with this error:

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. inspecting further I can see that clicking on this navbar link does lead to desired http://127.0.0.1:8000/questions.html.html page with desired view.
<a class="nav-link" href="{% url 'questions' %}">Top Questions</a>

however, social auth leads to the same error message in step 6.

further reading:

“Django URLS documentation.” Django, https://docs.djangoproject.com/en/5.0/topics/http/urls/. Accessed 9 Dec. 2023.

1

There are 1 best solutions below

0
Laurie Crean On

as is suggested in the error message in step 6, questions.html is not a valid urlpattern. urlpatterns do not recognise file names such as .html.

  1. I have updated the template to use no /questions/ instead of /questions.html.

     <a href="{% provider_login_url 'google' %}?next=/questions/">Login With Google</a>
    
  2. I have added a name to the urlpattern in users/urls.py

#users/urls.py
    path("questions/", views.logintoquestions, name='questions'), # added name='questions'
#users/views.py
def logintoquestions(request):
    return redirect("questions.html")