/ in urlpatterns django

Viewed 38

I am a beginner in django and don't understand the meaning "/" in urlpatterns. Here is the urlpatterns in my project urls. My first app's name is first.

urlpatterns = [
    path('admin/', admin.site.urls),
    path('first', include("first.urls"))
]

I found that it didn't work unless I amended it to following:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('first/', include("first.urls"))
]

I don't understand what the meaning of '/' is. I googled but did not find an answer. Could anyone help me on this?

Another question is that the double quotes and single quote could be used either way. But is there any convention or better practice that I can follow to use single or double quote? Thanks a lot.

1 Answers

Short answer: it is used as a separator for the first and for the URL paths in your first app.

You are including the urlpatterns of the first app. Imagine that this app has as URL:

# first/urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('foo/', foo)
]

Without the slash, the path to trigger foo is /firstfoo/ so it appends the two.

If you work with a slash, the path to trigger foo is /first/foo.

Related