Why does my Django project open the same home page irrespective or which url I type?

28 views
Skip to first unread message

Alex Callaway

unread,
Nov 12, 2018, 8:49:22 PM11/12/18
to Django users
In my urlpatterns in the url.py file of my app, I have two pages:

from django.urls import path, re_path
from django.conf.urls import url
from . import views
from django.contrib.auth.views import LoginView

urlpatterns=[
re_path(r'^$',views.home, name='home'),
path('login/', LoginView.as_view(template_name='accounts/login.html'), name='login'),
]

When I visit : `http://127.0.0.1:8000/home` I see whats in my `index.html`. 

But when I visit : 


I still see what's in my `index.html`. Wtf?


Ok. I did say in my views.py I have told it to render `home/index.html` like this :

def home(request):
return render(request, 'home/index.html')

But I also have this function in views.py as well :


def login(request):
c = {}
c.update(csrf(request))
return render(request, 'accounts/login.html', c)

So what's the problem? Why won't it render `accounts/login.html` page when I visit `http://127.0.0.1:8000/accounts/login`

By the way, In my main url.py file in the project, I have url patterns this way (every django project has two urls.py):
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include('clientview.urls')),
path('accounts/login/', include('clientview.urls')),
]

Jason

unread,
Nov 12, 2018, 10:06:37 PM11/12/18
to Django users
you have an ending slash on your login urls, and no ending slash in your links defined.  try adding a trailing slash

Alex Callaway

unread,
Nov 13, 2018, 6:16:17 PM11/13/18
to Django users
Thanks!

Manjunath

unread,
Nov 14, 2018, 10:29:35 PM11/14/18
to Django users
Also,
It would be better if you rearrange your patterns.

urlpatterns=[
path('login/', LoginView.as_view(template_name='accounts/login.html'), name='login'),
re_path(r'^$',views.home, name='home'),
]

Since your path r'^$' matches all request, it is better to keep this at end of your list.
Reply all
Reply to author
Forward
0 new messages