简体   繁体   中英

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'),
]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM