Discussion:
Why does my Django project open the same home page irrespective or which url I type?
Alex Callaway
2018-11-13 01:49:22 UTC
Permalink
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 :

`http://127.0.0.1:8000/accounts/login` or
`http://127.0.0.1:8000/accounts/login.html` or
`http://127.0.0.1:8000/accounts/login/login` or
`http://127.0.0.1:8000/accounts/login/login/login.html`

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')),
]
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b14297d2-a879-4642-9c75-82b103297f00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jason
2018-11-13 03:06:36 UTC
Permalink
you have an ending slash on your login urls, and no ending slash in your
links defined. try adding a trailing slash
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b288b73f-f10b-4ca8-9de0-892cef0a800f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alex Callaway
2018-11-13 23:16:17 UTC
Permalink
Thanks!
Post by Jason
you have an ending slash on your login urls, and no ending slash in your
links defined. try adding a trailing slash
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cd52e49f-16d0-4d9e-8575-5434d2caca8d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Manjunath
2018-11-15 03:29:35 UTC
Permalink
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.
Post by Alex Callaway
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` <http://127.0.0.1:8000/home>
I see whats in my `index.html`.
`http://127.0.0.1:8000/accounts/login`
<http://127.0.0.1:8000/accounts/login> or
`http://127.0.0.1:8000/accounts/login.html`
<http://127.0.0.1:8000/accounts/login.html> or
`http://127.0.0.1:8000/accounts/login/login`
<http://127.0.0.1:8000/accounts/login/login> or
`http://127.0.0.1:8000/accounts/login/login/login.html`
<http://127.0.0.1:8000/accounts/login/login/login.html>
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`
return render(request, 'home/index.html')
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`
<http://127.0.0.1:8000/accounts/login>
By the way, In my main url.py file in the project, I have url patterns
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include('clientview.urls')),
path('accounts/login/', include('clientview.urls')),
]
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2622b5d3-8bcd-4277-a94e-1df2a11e8023%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...