Django url pattern - string parameter

Django url pattern that have a number parameter is:

url(r'^polls/(?P<poll_id>\d+)/$', 'polls.views.detail')

What will be the correct syntax if my poll_id is not a number but a string of character?


Solution 1:

for having a string parameter in url you can have: url like this:

url(r'^polls/(?P<string>[\w\-]+)/$','polls.views.detail')

This will even allow the slug strings to pass eg:strings like node-js etc.

Solution 2:

In newer versions of Django such as 2.1 you can use

path('polls/<str:poll_id>', views.polls_detail)

as given here Django URL dispatcher

def polls_detail(request,poll_id):
#process your request here