Naim Blg
Naim Blg

Reputation: 131

Django 1.11 url pattern error, how to solve?

When I try to fix the url in my urlpatterns it shows me this error :

The error:

Your URL pattern "url(r'^player/[?P[-\w\x20]+]/$', PlayerDetailView.as_view(), name='player-detail-view')," is invalid. Ensure that urlpatterns is a list of url() instance. try removing the string 'url(r'^player/[?P[-\w\x20]+]/$', PlayerDetailView.as_view(), name='player-detail-view'),'. The list of urlpatterns should not have a prefix string as the first element.*

My Code :

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', HomePageView.as_view(), name='home-page'),
    url(r'^teams/$', TeamsListView.as_view(), name='teams-list-view'),
    url(r'^scores/$', ScoresListView.as_view(), name='scores-list-view'),
    url(r'^player/[?P<slug>[-\w\x20]+]/$', PlayerDetailView.as_view(), name='player-detail-view'),
]

Can anyone help me??

Upvotes: 1

Views: 978

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477794

The syntax is a bit off, you need to use round brackets (..) instead of square brackets [..] around a "capture group":

url(
    r'^player/(?P<slug>[-\w\x20]+)/$',
    PlayerDetailView.as_view(),
    name='player-detail-view'
),

Furthermore if I recall correctly, a slug can not contain spaces, so you might want to remove the \x20.

Note that in and higher, the path(..) [Django-doc] function can be used, which has support for slugs like:

# Django 2.0 and higher

path('player/<slug:slug>/', PlayerDetailView.as_view(), name='player-detail-view'),

Then Django replaces the slug with a builtin pattern, which makes the URL patterns more "declarative".

Upvotes: 1

Related Questions