TIMEX
TIMEX

Reputation: 271584

How do I write this URL in Django?

(r'^/(?P<the_param>[a-zA-z0-9_-]+)/$','myproject.myapp.views.myview'),

How can I change this so that "the_param" accepts a URL(encoded) as a parameter?

So, I want to pass a URL to it.

mydomain.com/http%3A//google.com

Edit: Should I remove the regex, like this...and then it would work?

(r'^/(?P[*]?)/?$','myproject.myapp.views.myview'),

Upvotes: 3

Views: 378

Answers (3)

Felix Kling
Felix Kling

Reputation: 816232

Add % and . to the character class:

[a-zA-Z0-9_%.-]

Note: You don't need to escape special characters inside character classes because special characters lose their meaning inside character sets. The - if not to be used to specify a range should be escaped with a back slash or put at the beginning (for python 2.4.x , 2.5.x, 2.6.x) or at the end of the character set(python 2.7.x) hence something like [a-zA-Z0-9_%-.] will throw an error.

Upvotes: 2

zerofuxor
zerofuxor

Reputation: 336

I think you can do it with:

(r'^(?P<url>[\w\.~_-]+)$', 'project.app.view')

Upvotes: 0

Seth
Seth

Reputation: 46403

You'll at least need something like:

(r'^the/uri/is/(?P<uri_encoded>[a-zA-Z0-9~%\._-])$', 'project.app.view'),

That expression should match everything described here.

Note that your example should be mydomain.com/http%3A%2F%2Fgoogle.com (slashes should be encoded).

Upvotes: 0

Related Questions