How to urlquote backslash in reverse arguments

232 views
Skip to first unread message

Vitaly

unread,
Dec 17, 2013, 5:15:46 AM12/17/13
to django...@googlegroups.com
Hi 

I got an some incorrection in reverse behaviour. 

From django docs
The string returned by reverse() is already urlquoted. For example:

>>> reverse('cities', args=[u'Orléans'])
'.../Orl%C3%A9ans/'
Applying further encoding (such as urlquote() or urllib.quote) to the output of reverse() may produce undesirable results.
But why backslash is not? How to make it urlquoted to %2F in that case?

Python 2.7.6 (default, Nov 26 2013, 12:47:31) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.urlresolvers import reverse
>>> reverse('events-index', kwargs={'tag': 'PL/SQL'})
'/calendar/tags/PL/SQL/'
>>> reverse('events-index', kwargs={'tag': 'PLЄ/SQL'})
'/calendar/tags/PL%D0%84/SQL/'
>>> 

Tom Evans

unread,
Dec 17, 2013, 7:28:15 AM12/17/13
to django...@googlegroups.com
On Tue, Dec 17, 2013 at 10:15 AM, Vitaly <has...@gmail.com> wrote:
> Hi
>
> I got an some incorrection in reverse behaviour.
>
> From django docs
>
> The string returned by reverse() is already urlquoted. For example:
>
>>>> reverse('cities', args=[u'Orléans'])
> '.../Orl%C3%A9ans/'
> Applying further encoding (such as urlquote() or urllib.quote) to the output
> of reverse() may produce undesirable results.
>
> But why backslash is not? How to make it urlquoted to %2F in that case?

Why *would* a backslash be quoted?

Neither django, nor python's urllib.quote would replace a backslash by
default, as it is not normally required.

Lets put it another way, what are you trying to do that causes
backslashes not being escaped to be a problem?

Cheers

Tom

Bill Freeman

unread,
Dec 17, 2013, 10:29:39 AM12/17/13
to django-users
I wonder if the OP is calling the forward slashes (of just slashes or solidus) in his example with back slash (reverse solidus), like much of the media does (curse Microsoft for using back slash as a path separator).  The %2F of which he speaks does go with slash.  (Back slash would be %5C.)

Url-quoting is a scheme to replace characters which might not be transmitted correctly as part of a UR.  Slash has no such problem.  You may as well ask why the letter 't' doesn't get replaced.

If you have a separate reason to quote slashes, you can use the replace method of strings:

    reverse(...).replace('/', '%2f')

But you don't need (or want) to do that to get a URL that will access the given view.

Bill



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1LDvsT-3z2NekpWHdetage14SMLsVYpjOMaK2wtLYpbJw%40mail.gmail.com.

Peter of the Norse

unread,
Jan 15, 2014, 2:33:43 PM1/15/14
to django...@googlegroups.com
Someone will have to correct me if I'm wrong, but it might be intentional. Some recent versions of Apache fail hard when confronted with an encoded slash. Some decode it before passing it to WSGI, while others don't. And then there are the versions that return 500 or 400 errors. And then there are the other servers that try to emulate the behavior without consideration.

I just checked, and percent signs, question, and hash marks (%?#) aren't encoded either. So, yeah, that's a bug.


On 12/17/2013 3:15 AM, Vitaly wrote:
But why backslash is not? How to make it urlquoted to %2F in that case?

Python 2.7.6 (default, Nov 26 2013, 12:47:31) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.urlresolvers import reverse
>>> reverse('events-index', kwargs={'tag': 'PL/SQL'})
'/calendar/tags/PL/SQL/'
>>> reverse('events-index', kwargs={'tag': 'PLЄ/SQL'})
'/calendar/tags/PL%D0%84/SQL/'
>>> 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/groups/opt_out.
-- 
Peter of the Norse
Reply all
Reply to author
Forward
0 new messages