Asma Gheisari
Asma Gheisari

Reputation: 6294

have unicode characters in url

for example in this url:

(r'^company/لیست-مقالات/$','CompanyHub.views.docList')

in لیست مقالات part I have unicode characters but I get this error:

Caught UnicodeDecodeError while rendering: 'ascii' codec can't decode byte 0xd9 in position 0: ordinal not in range(128)

Upvotes: 3

Views: 786

Answers (2)

jfs
jfs

Reputation: 414825

First, to use non-ASCII characters in a string literal in Python you need to specify a character encoding at the top of your source file e.g.:

# -*- coding: utf-8 -*-

Second, if you pass a Unicode string to django it usually does the right thing by itself. In this case it should convert all non-ASCII characters in the URL. If it is not then you could call django.utils.encoding.iri_to_uri() explicitly.

Upvotes: 1

Alex W
Alex W

Reputation: 38253

You need to put a u in front of r before the string, or wrap it in the unicode method:

See this for more info.

Unicode strings are much like strings, but are specified in the syntax using a preceding 'u' character: u'abc', u"def".

http://docs.python.org/library/stdtypes.html

Upvotes: 2

Related Questions