Skip to content
Advertisement

Django urls.py issue, not sure what to put in ‘urlpatterns’

I’ve been following this tutorial (Django Tutorial Part 3) and I’ve stumbled across what I’m assuming is a syntax issue between the time this tutorial was written and the new Django releases. My admin page loads just fine.

Code Block in Question So in the picture linked, that is the area that I am having trouble with from the tutorial. I think my “mysite/urls.py” file is fine, but the “polls/urls.py” file is where I’m not sure what to put.

Below is what mysite/urls.py looks like:

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

urlpatterns = [
     path('admin/', admin.site.urls),
     path('polls/', include('polls.urls')),

And here is what my polls/urls.py looks like:

from django.urls import path
from . import views

urlpatterns = [
    path(**??? not sure what to put here**, views.index, name='index'),
]

Advertisement

Answer

but the polls/urls.py file is where I’m not sure what to put.

The empty string (''), so:

urlpatterns [
    #    ↓↓ empty string
    path('', views.index, name='index')
]

We can inspect how the path is compiled to a regex with:

>>> from django.urls import path
>>> pt = path('', lambda x: x)
>>> pt.pattern.regex
re.compile('^$')

The pattern is thus compiled to a regex ^$, which is the same as the regex for the url pattern in the documentation.

Advertisement