From the Django source, urlencode is basically a wrapper around Django's urlquote utility method. From the comments in the source, urlquote is a UTF-8-safe version of urllib.quote.
So urlencode is using the same defaults as python's urllib.quote, and the reason that urllib.quote does not escape slashes can be found in the documentation:
Replace special characters in string using the %xx escape. Letters, digits, and the characters '_.-' are never quoted. By default, this function is intended for quoting the path section of the URL. The optional safe parameter specifies additional characters that should not be quoted — its default value is '/'.
So, the reason is that it's escaping the path, and '/' is a perfectly expected and valid character within a path.
Hello i would like to encode an url like this:
http://www.foodserv.co.za
I tried it with the following code
import urllib link = urllib.parse.quote(link) print(link)
But with that i only get: http%3A//www.foodserv.co.za
But i would like to have also the // encoded and in the end i should look like this: http%3A%2F%2Fwww.foodserv.co.za
Is there some other way or parameter to encode the url like i need it?
python - Why does Django's `urlencode` not encode slash? - Stack Overflow
Percent encoded forward slash is stil treated as a URL component delimiter
URL slash encoded
How can I percent-encode URL parameters in Python? - Stack Overflow
From the Django source, urlencode is basically a wrapper around Django's urlquote utility method. From the comments in the source, urlquote is a UTF-8-safe version of urllib.quote.
So urlencode is using the same defaults as python's urllib.quote, and the reason that urllib.quote does not escape slashes can be found in the documentation:
Replace special characters in string using the %xx escape. Letters, digits, and the characters '_.-' are never quoted. By default, this function is intended for quoting the path section of the URL. The optional safe parameter specifies additional characters that should not be quoted — its default value is '/'.
So, the reason is that it's escaping the path, and '/' is a perfectly expected and valid character within a path.
To get urlencode to also escape / in a Django template, use {{ variable|urlencode:'' }}.
Explanation: The extra optional parameter tells urlencode the set of characters that are "safe", where the default is '/', so passing an empty string is telling urlencode that / is not safe and should be encoded.
From the Python 3 documentation:
Copyurllib.parse.quote(string, safe='/', encoding=None, errors=None)
Replace special characters in string using the
%xxescape. Letters, digits, and the characters'_.-~'are never quoted. By default, this function is intended for quoting the path section of a URL. The optional safe parameter specifies additional ASCII characters that should not be quoted — its default value is'/'.
That means passing '' for safe will solve your first issue:
Copy>>> import urllib.parse
>>> urllib.parse.quote('/test')
'/test'
>>> urllib.parse.quote('/test', safe='')
'%2Ftest'
(The function quote was moved from urllib to urllib.parse in Python 3.)
By the way, have a look at urlencode.
About the second issue, there was a bug report about it and it was fixed in Python 3.
For Python 2, you can work around it by encoding as UTF-8 like this:
Copy>>> query = urllib.quote(u"Müller".encode('utf8'))
>>> print urllib.unquote(query).decode('utf8')
Müller
In Python 3, urllib.quote has been moved to urllib.parse.quote, and it does handle Unicode by default.
Copy>>> from urllib.parse import quote
>>> quote('/test')
'/test'
>>> quote('/test', safe='')
'%2Ftest'
>>> quote('/El Niño/')
'/El%20Ni%C3%B1o/'