-3

Using django 1.7 and python 2.7, in a views I have:

page = 0
sex = [u'\u0632\u0646'] #sex = زن
url = "/result/%s/%d"  % (sex, page)
return HttpResponseRedirect(url)

Which needs to return:

/result/زن/0

However the resulting url turns out to be:

/result/[u'\u0632\u0646']/0

Which is not what envisage in the pattern:

`url(r'^result/(?P<sex>\w+)/(?P<page>\d+)','userprofile.views.profile_search_result')`,

I also tried

return HttpResponseRedirect( iri_to_uri(url))

but does not solve the problem.

I got really confused and appreciate your help to fix this.

supermario
  • 2,625
  • 4
  • 38
  • 58
  • But seems that `sex` is the second part of your pattern `r'^result/(?P\w+)/(?P\d+)'`? And about your `url` you need to specify for python that the format is unicode you you need to add a `u` at the leading of your url `url = u"/result/%s/%d" % (sex, page)` – Mazdak Mar 06 '15 at 09:47
  • or in return use `unicode` function `return HttpResponseRedirect(unicode(url,'escape-unicode'))` – Mazdak Mar 06 '15 at 09:53
  • @Kasra, the intended first parameter after `/result` is `زن` as is evident in url pattern. However browser (and text editor here) moves `0` to the left of `زن` (not sure why). – supermario Mar 06 '15 at 10:03
  • @Kasra `return HttpResponseRedirect(unicode(url,'escape-unicode'))` causes: `decoding Unicode is not supported` – supermario Mar 06 '15 at 10:09
  • Yes it could be done , although my consul show it right `>>> print url /result/زن/0 ` . :) sorry decoding is `'unicode-escape'`! but i think this could be relevant to your browser too! for more detail check this link https://docs.djangoproject.com/en/1.7/ref/unicode/#uri-and-iri-handling – Mazdak Mar 06 '15 at 10:09
  • Your claimed result is *not* what you get when you run that code. The code runs perfectly and gives the expected result: `/result/زن/0`. You would only get the result that you show if `sex` is a list. – Daniel Roseman Mar 06 '15 at 10:15
  • @DanielRoseman You are right. the sex was indeed a list: `[u'\u0632\u0646']` . I modified my question. – supermario Mar 06 '15 at 10:22

2 Answers2

1

url should also be an unicode string for that to work:

page = 0
sex = u'\u0632\u0646' #sex=زن
url = u"/result/%s/%d"  % (sex, page)
return HttpResponseRedirect(url)
GwynBleidD
  • 20,081
  • 5
  • 46
  • 77
1

Since sex is a list, you simply need to use the actual element you want:

url = "/result/%s/%d"  % (sex[0], page)

Although note that to construct URLs in Django, you should really use the reverse function:

from django.core.urlresolvers import reverse
...
url = reverse('userprofile.views.profile_search_result', kwargs={'sex': sex[0], 'page': page})
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895