I know there is existing title about this, but there question is different from mine. So here's my problem. I use context processor to display user name. It's working but my sentry detect an error yesterday.
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in range(128)
Here is my code:
def display_name(request):
try:
name = "{0} {1}".format(request.user.first_name, request.user.last_name)
name = name.strip()
if not name:
name = request.user.username
except AttributeError:
name = None
return {'display_name': name,}
What's the cause of this? Or the user input character for their name?
It's basically a user input problem.
Text encodings are a whole "thing" and hard to get into, but in a nut shell, a user entered a Unicode character that can't easily be mapped to an ASCII character.
You can fix this by changing this:
name = "{0} {1}".format(request.user.first_name, request.user.last_name)
To this:
name = u"{0} {1}".format(request.user.first_name, request.user.last_name)
This tells Python to treat the string as a unicode string (which has all the normal functions as an ascii string).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With