67

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?

7 Answers 7

68

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
Sign up to request clarification or add additional context in comments.

4 Comments

with this answer you can basically then request 'url.com/polls/123'. How can one create a url such that you can send a request 'url.com/polls?poll_id=123' ?
Just create a path with polls and then you can add anything in query params.
note the difference! It is path now, not url! It took me so long...
56

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.

Comments

36

Depends on what characters you care about. Like the docs say, \w will give you an alphanumeric character or an underscore.

1 Comment

thanks man! i have try using \s i didn't know its \w. thank you again.
20

Starting in Django 2.0 it is easier to handle string parameters in URLs with the addition of the slug symbol, which is used just like int in urls.py:

from django.urls import path

urlpatterns = [
    path('something/<slug:foo>', views.slug_test),
]

And in your function-based or class-based view you would handle it just like any other parameter:

def slug_test(request, foo):
    return HttpResponse('Slug parameter is: ' + foo)

1 Comment

The slug type won't work if the name contains underscores, cuz the slug expects it to be hyphen-separated. But it's a good answer if that's what you want
10

From Django 2.0 onward, path has been introduced. path does not take reg ex in urls, hence it is meant to be a simplified version of the older url

From 2.0 onward you can use path instead like below :

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

string path parameters need not be explicitly specified, as default data type for path parameters is string itself.

Ref : https://docs.djangoproject.com/en/2.0/releases/2.0/#whats-new-2-0

Comments

1

If you are using Django version >= 2.0, then this is done simply like below.

from django.urls import path    

urlpatterns = [
    ...
    path('polls/<string>/$','polls.views.detail')
    ...
]

Source: https://docs.djangoproject.com/en/2.0/ref/urls/#django.urls.path

1 Comment

There is slug, int and username in angle brackets, but not string. And a datatype must be followed by : and the keyword I think. So your example might be wrong.
1

In case your angle-bracket argument is a path, i.e. contains "/", you'll have to use path:.

Example:
path('section/<path:some_path>,app.views.some_view,name='some_name')

Source: https://docs.djangoproject.com/en/4.0/topics/http/urls/#path-converters

(EDITED)
"str - Matches any non-empty string, EXCLUDING the path separator, '/'. This is the default if a converter isn’t included in the expression.

slug - Matches any slug string consisting of ASCII letters or numbers, plus the hyphen and underscore characters. For example, building-your-1st-django-site.

path - Matches any non-empty string, INCLUDING the path separator, '/'. This allows you to match against a complete URL path rather than a segment of a URL path as with str."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.