[Django]-Django unicode error on admin page

43đź‘Ť

âś…

There’s a ’ character somewhere, probably in self.project.name. You could probably find it if you check the whole error message.

However, if you’re getting unicode results from your database it would probably be smarter to do something like this:

def __str__(self):
    return ('Proposal for: %s' % self.project.name).encode('ascii', errors='replace')

The smartest thing to do, since it’s recommended by the Django documentation, is to implement the __unicode__ function instead:

def __unicode__(self):
    return u'Proposal for: %s' % self.project.name

8đź‘Ť

2019 is RIGHT SINGLE QUOTATION MARK, commonly used as a curly apostrophe.

The problem probably is caused by you using __str__ instead of __unicode__, and Django’s documentation recommends that you only use __unicode__.

The list of instances probably shows up fine because it doesn’t include the field that contains the apostrophe.

👤Jim

5đź‘Ť

(I’d add this as a comment to Andre’s but don’t have the 50 points yet)

This:

def __unicode__(self):
    return 'Proposal for: %s' % self.project.name

Should be

def __unicode__(self):
    return u'Proposal for: %s' % self.project.name

This is especially true if you are using a variable in the definition that references another model that may return a string with characters unicode doesn’t like. Putting in the “u” in front of the text returned makes sure everything is kosher and returned as unicode.

👤Pstrazzulla

Leave a comment