Django users,
In my Django app, I want to allow users to enter Unicode
strings. But if they do, I get UnicodeEncodeError when I call
format() to embed their string into other strings. Is there an
easy way to tolerate the Unicode?
Details:
After prompting the user for name, I may have a line of code like:
log('Name entered was: {0}'.format(name))
With Python 2.7.3 and Django 1.4.3, this raises exception
UnicodeEncodeError if the user entered a string containing
Unicode chars, because it tries to embed Unicode chars into
my ASCII string template. So, I have to I change it to:
log(
u'Name entered was:
{0}'.format(name))
I have many thousands of such lines in my 200,000+ lines of
Python and Django code.
Is there a master switch somewhere that would cause a Python
2.x string literal to default to Unicode instead of ASCII, as it
does in Python 3?
Or do I have to explicitly change '' to u'' in all such places?
I looked into adding a line like this to the top of the file, but
no luck because it seems to only affect the encoding of Unicode
string literals (u''), not regular string literals (''):
# -*- coding: utf-8 -*-
Any suggestions? Much appreciated. Thanks!