Jesse Vermeulen Jesse Vermeulen - 2 months ago 12
Python Question

Django redirecting everything to homepage

I'm stuck with a Django project, I tried to add another app called login to make a login page but for some reason the page just redirects to the homepage except for the admin page

For example: 127.0.0.1:8000 will go to the homepage but 127.0.0.1:8000/login will also display the homepage even though I linked another template to it.

Here is my code:

main urls.py

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('portal.urls')),
url(r'^login/', include('login.urls')),
]


login urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^login/', views.index, name="login"),
]


login views.py

from django.shortcuts import render

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


portal urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^', views.index, name="portal"),
]

J F J F
Answer

I see 2 problems here:

  1. As @DanielRoseman mentioned above, the regular expression ^ matches anything, so you should change it to ^$.
  2. When you use an include, the rest of the path after what the include matched is passed to the included pattern. You’ll want to use ^$ in your login urls.py too.