Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 'ascii' codec can't encode character

In Django I want to use a simple template tag to truncate data.

This is what I have so far:

@register.filter(name='truncate_simple')
def truncate_char_to_space(value, arg):
    """
    Truncates a string after a given length.
    """
    data = str(value)
    if len(value) < arg:
        return data

    if data.find(' ', arg, arg+5) == -1:
        return data[:arg] + '...'
    else:
        return data[:arg] + data[arg:data.find(' ', arg)] + '...'

But when I use it I get the following error:

{{ item.content|truncate_simple:5  }}

Error:

'ascii' codec can't encode character u'\u2013' in position 84: ordinal not in range(128)

Error is on line starting data = str(value)

Why?

like image 596
GrantU Avatar asked Jul 31 '13 15:07

GrantU


1 Answers

If you're using django and python 2.7 this fixes it for me:

from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class Utente(models.Model):

see https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.encoding.python_2_unicode_compatible

like image 110
max4ever Avatar answered Sep 21 '22 12:09

max4ever