views:

47

answers:

2

code:

class Gallery(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField(blank=True)
    created = models.DateField(auto_now_add=True)    
    class Meta:
        verbose_name = 'галерея'
        verbose_name_plural = 'галереи'
    def __unicode__(self):
        return 'Галерея %s' % self.title

error:

TemplateSyntaxError at /admin/galleries/gallery/
Caught an exception while rendering: ('ascii', '\xd0\x93\xd0\xb0\xd0\xbb\xd0\xb5\xd1\x80\xd0\xb5\xd1\x8f ', 0, 1, 'ordinal not in range(128)')

what should I do?

+1  A: 

Try

return u'Галерея %s' % self.title
WooYek
This is the number one Django error I see. Rule of thumb: `__unicode__` must *always* return unicode.
Daniel Roseman
A: 

Because self.title is a unicode string and your string literal is not (its type is str), when Python executes the expression 'Галерея %s' % self.title, it needs to coerce the string literal into unicode before performing the interpolation. It needs a character encoding to do this, and by default resorts to ASCII, which can encode less than 128 different characters.

To avoid this problem, use a unicode string literal : u'Галерея %s'. When in doubt, your strings should be unicode strings, especially in Django.

jd