[Django]-Why is Django reverse() failing with unicode?

5👍

In your to_url method, you need to make sure the keys in the d dict are not Unicode strings. This isn’t peculiar to Django, it’s just how keyword arguments to functions work in Python. Here’s a simple example:

>>> def f(**kwargs): print kwargs
... 
>>> d1 = { u'foo': u'bar' }
>>> d2 = { 'foo': u'bar' }
>>> f(**d1)
TypeError: f() keywords must be strings
>>> f(**d2)
{'foo': u'bar'}

Changing your

d = dict( self.arguments.values_list('key', 'value') )

into something like

d = dict((str(k), v) for k, v in self.arguments.values_list('key', 'value').iteritems())

should do the trick.

Leave a comment