On Saturday 27 June 2009 12:16:24 am hugo wrote:
> Hi everyone,
>
> <a href="{{myURL | urlencode}}">Click this</a>
>
> I want "Click this" to have hyperlink "myURL". However, the link
> produced by the code above became
> http://mySite/myURL instead of myURL. So if myURL is http://www.time.com,
> the resulting link becomes http://mySite/http://time.com, which is
Interesting. What version of django are you using? In 1.1beta, the site URL
is not prepended in my test installation.
However, it still does not do what you want; you would only want to encode a
portion of the url, not the whole thing. The url encoding
of "http://time.com" is "http%3A//time.com".
Not what you want. Right?
In this example, you'd just want to say in the template:
<a href="{{myURL|safe}}">Click this</a>
(The safe filter is needed in case there are any query params,
e.g. "http://time.com?x=1&y=2" - you don't want that "&" to become "&",
etc. See the template docs for the "safe" and "escape" filters if you're not
sure what this means.)
Now, if you truly do need to urlencode a path in a URL, you'd need to apply
that separately. E.g. to get the result "http://time.com/who%20is%20he%3F",
you want to urlencode "who is he?" only, and leave the rest of the URL
unmolested.
The simplest way is probably to do the processing in the view code. Use
python's urllib.quote or urllib.quote_plus functions on the path, append it
to the toplevel domain url to create the full myURL. Then pass myURL
directly to the template as above (again, remember the "safe" filter). This
is the best approach for you most likely.
Cheers,
Aaron
--
Aaron Maxwell
http://redsymbol.net/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---