Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#20022 closed Bug (fixed)

reverse() breaks on URLs which include a ~ character

Reported by: mxheyeck@… Owned by: Baptiste Mispelon
Component: Core (URLs) Version: 1.5
Severity: Normal Keywords:
Cc: bmispelon@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Testing on my local machine through Apache, so the root URL is at:

http://localhost/~mxheyeck/sicariid/

Calling reverse on one of my views inside views.py, e.g.:

s = reverse(locate_task_enter, args=(task_id,))

Breaks with the cryptic message:

"a float is required"

A little examination reveals what's going on. Break is at line 391 of urlresolvers.py.

candidate = (prefix_norm + result) % dict(zip(prefix_args + params, unicode_args))

prefix_norm is:

u'/%7Emxheyeck/sicariid/'

So line 391 is equivalent to:

u'/%7Emxheyeck/sicariid/locate_task_enter/%(task_id)s/' % {u'task_id': u'419'}

Where '%7E' is both a urlencoded tilde and, unfortunately, an upper-case floating-point exponential. So, boom.

I attach the local vars from the break at 391.

val		419L
pattern		'locate_task_enter/(?P<task_id>\\d+)/$'
self		<RegexURLResolver 'sitespecific.urls' (None:None) ^/>
args		(419L,)
_prefix		u'/~mxheyeck/sicariid/'
possibility	[(u'locate_task_enter/%(task_id)s/', [u'task_id'])]
unicode_args	[u'419']
possibilities	[([(u'locate_task_enter/%(task_id)s/', [u'task_id'])],  'locate_task_enter/(?P<task_id>\\d+)/$',  {})]
lookup_view	<function locate_task_enter at 0x1065f8668>
prefix_norm	u'/%7Emxheyeck/sicariid/'
prefix_args	[]
params		[u'task_id']
result		u'locate_task_enter/%(task_id)s/'
defaults	{}
kwargs		{}

Change History (5)

comment:1 by Aymeric Augustin, 12 years ago

Component: UncategorizedCore (URLs)
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 by Baptiste Mispelon, 12 years ago

Cc: bmispelon@… added
Owner: changed from nobody to Baptiste Mispelon
Status: newassigned

This seems to have been overlooked when fixing #18210.

The fix should be similar: escape any % present in the prefix.

Note that a temporary workaround is to used named arguments (kwargs) for reverse.

comment:3 by Baptiste Mispelon <bmispelon@…>, 12 years ago

Resolution: fixed
Status: assignedclosed

In 4fa7f3cdd9bcf50ec4c7f64a31c1dfc02c375f46:

Fix #20022: Correctly handle prefixes with url-unsafe characters in reverse().

comment:4 by Jacob Kaplan-Moss <jacob@…>, 12 years ago

In e4d9f8aed11e964885cd77f8abd01ba0dfa4aa13:

Merge pull request #900 from bmispelon/ticket-20022

Fix #20022: Correctly handle prefixes with url-unsafe characters in reverse()

comment:5 by Florian Apolloner <florian@…>, 12 years ago

In 1059da8de675442e84381d6366c0be254681753e:

Merge pull request #900 from bmispelon/ticket-20022

Fix #20022: Correctly handle prefixes with url-unsafe characters in reverse()

Note: See TracTickets for help on using tickets.
Back to Top