1

How can I create a url pattern for two parameters where the first parameter contains a forward slash as part of its contents:

da/ta1=/data2

Intially I had the following pattern:

(r'^view/(?P<item_id>\w+=)/(?P<changekey>\w+)/$', 'view'),

However this pattern does not match because of the first forward slash which is part of the parameter data.

2 Answers 2

2

Assuming you construct the url yourself, you could use quote_plus to encode the inline forward slash:

>>> '/'.join([urllib.quote_plus(d) for d in ['da/ta1', 'data2']])
'da%2Fta1/data2'

And to decode:

>>> urllib.unquote_plus('da%2Fta1/data2')
'da/ta1/data2'

To then match your data, your pattern could be changed to the construct found below. For the first parameter, this matches everything up to the = character; the second parameter is expected to be alphanumerical.

(r'^view/(?P<item_id>[^=]+)=/(?P<changekey>\w+)/$', 'view')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that, the only issue I'm having now is that the pattern still isn't matching, how can I get the pattern to include %2F when matching?
1

Django Admin does have the same problem with slashes in parameters. To fix this, Django uses its own quote function:

from django.contrib.admin.utils import quote
quote(param1)

In the view itself:

unquote(self.kwargs.get('param1'))

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.