django url from a django app is giving 404 error

Question:

demo > url.py (This is main django project url file)

from django.contrib import admin
from django.urls import path, include
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('about', views.about, name='about'),
    path('contact', views.contact, name='contact'),    
    path(r'^user/', include('user.urls')), 
]

user > url.py (this is a user app inside django project’s url file)

from django.urls import path
from . import views

urlpatterns = [
    path(r'^/login', views.login, name='login'),
]

now i am trying to open http://127.0.0.1:8000/user/login but its giving me 404 error.

Asked By: Mohit

||

Answers:

You don’t use a regex for path(…) [Django-doc], for regexes, you use re_path(…) [Django-doc]. But here it is not necessary: you can define this with path(…):

from django.contrib import admin
from django.urls import path, include
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('about', views.about, name='about'),
    path('contact', views.contact, name='contact'),
    path('user/', include('user.urls')),
]

and for user/urls.py:

urlpatterns = [
    path('login/', views.login, name='login'),
]
Answered By: Willem Van Onsem
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.