Tim,

Excellent response, thanks!  The link to the doc is just what I
needed.

One more question.  I'm still getting UnicodeEncodeError on
some calls to str() because I should now be calling unicode()
instead.  Any easy way to map all of my str() calls to unicode()
call?

Thanks!
--Fred
Fred Stluka -- mailto:f...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.
On 4/14/16 10:05 PM, Tim Graham wrote:
You want `from __future__ import unicode_literals`: https://docs.djangoproject.com/en/dev/ref/unicode/#general-string-handling

On Thursday, April 14, 2016 at 6:27:07 PM UTC-4, Fred Stluka wrote:
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!

--Fred
Fred Stluka -- mailt...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3da21b79-4bfd-4f05-bbc4-579505365376%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/57184BE1.1030505%40bristle.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to