guettli
guettli

Reputation: 27796

Django: Parameters in URLs .... which start with a slash

I use a regex for URL dispatching like described in the Django docs.

r'^remote/(?P<slug>[^/]+)/call_rfc/(?P<rfc_name>.+)$'

Unfortunately rfc_name can start with a slash!

Example:

https://example.com/remote/my_slug/call_rfc//MLK/FOO

The rfc_name is /MLK/FOO.

But this fails. Somewhere (I don't know yet if it is in the browser or in Django) the duplicate slash gets removed.

What is the best practice to handle URL parameters which can start with a slash?

Upvotes: 0

Views: 115

Answers (1)

Higor Rossato
Higor Rossato

Reputation: 2046

It almost seems that you can consider the latest "slug" as a path. If that's the case, in your URL definition you can use path to represent that. You can have a look here to check if it helps.

path('remote/<slug:slug>/call_rfc/<path:rfc_name>', yourviewhere)

Or, you can perhaps write your custom path converter.

Upvotes: 1

Related Questions