David542
David542

Reputation: 110277

How django's url regex work

In the following url:

(r'^videos/view/(?P<video_id>[^/]+)/$'

In other words, how is the above different than:

'^/videos/view/[^/]+/$'

Upvotes: 2

Views: 252

Answers (1)

Danica
Danica

Reputation: 28846

r'' marks a raw string, so that you don't have to double-escape backslashes. In this case, it's not necessary because there aren't any, but a lot of people always do it for regexes anyway.

(?P<video_id>[^/]+) is a Python extension to regexes that "names" that capture group video_id. In Django, this means that the match is sent to the view as a keyword argument video_id; if you did view/([^/]+)/$, it would be sent as the first positional argument. In your example, though, there are no parens at all, meaning that the view wouldn't get any arguments!

Upvotes: 5

Related Questions