Tuesday, 17 September 2013

Cannot get django template tag to return unicode

Cannot get django template tag to return unicode

I'm writing an app that will track a series of matches between 2 players.
I am using Django's User model and extending it with my own UserProfile.
I store usernames in User as their steamID (ex: 76561197965801299) and
then look up their steam username on login, and update UserProfile.
Instead of looking at 76561197965801299, I want to look at a username, and
on one page, I want to decorate this username with more goodies, so I
wrote a template tag.
Problem:
I cannot seem to print unicode data from my template tag.
Actual Error:
'ascii' codec can't encode character u'\u260e' in position 16: ordinal not
in range(128)
Normally Django doesn't bother me with unicode issues (for example: I can
see this unicode object in the admin pages no problem) but I've never
tried applying a template tag, so there is obviously something I'm doing
wrong here.
template/ladder/match_game_listing.html
{{ match.challengee|steam_name }}
The match.challengee in this case is 76561197971597000.
ladder/templatetags/ladder_filters.py
from django import template
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.utils.html import mark_safe
from cafe.models import UserProfile
register = template.Library()
@register.filter()
def steam_name(name):
try:
user_obj = User.objects.get(username=name)
user_prof = UserProfile.objects.get(user_id=user_obj.id)
url = user_prof.url
handle = unicode(user_prof.handle)
avatar = user_prof.avatar
steam_string = "<a href='{0}' alt='{1}\'s profile'><img src='{2}'
alt='{1}\'s avatar' >{1}</a>".format(url, handle, avatar)
return mark_safe(steam_string)
# Non-steam entities can exist, ignore
except ObjectDoesNotExist:
return name
When I go to view this in the browser, I get the aforementioned error:
UnicodeEncodeError at /ladder/dota2/ 'ascii' codec can't encode character
u'\u260e' in position 16: ordinal not in range(128)
With a helpful hint of:
Unicode error hint
The string that could not be encoded/decoded was: oose ¢Ï
I've tried browsing the Django docs numerous times, and I have tried
playing with force_text() to no avail, but as I'm a little unclear on why
this isn't working, I might just be missing the relevant section. This
template tag works in cases where the name does not have unicode.

No comments:

Post a Comment