What does django urlpattern path argument empty (' ') meaning?

Viewed 40

what does path('') in urls.py from project mean?

This is urls.py from project.

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls'))
]

And this is urls.py from app

urlpatterns = [
    path('', views.index),
    path('create/', views.create),
    path('read/id/', views.read),
    ]

Is path('')in urls.py from project just mean empty?

If it is, why when I write localhost:8080/create

It will still work? I just routing path('') in urls.py from project Does it mean "except admin/" ..?

1 Answers

it means that the paths below myapp.urls will be added without any prefix, so that means that the paths will be:

admin/    … (all admin urls)
/         → views.index
create/   → views.create
read/id/  → views.read

likely however the path to read should contain a URL parameter, not id in the real path, so:

urlpatterns = [
    path('', views.index),
    path('create/', views.create),
    path('read/<int:id>/', views.read),
]

If you had added a value to the path, like:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('foo/', include('myapp.urls')),
]

then the paths would be:

admin/        … (all admin urls)
foo/          → views.index
foo/create/   → views.create
foo/read/id/  → views.read
Related