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?
Django URL pass parameter to view You can pass a URL parameter from the URL to a view using a path converter. Then “products” will be the URL endpoint. A path converter defines which type of data will a parameter store. You can compare path converters with data types.
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info . Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view).
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With