I'm vaguely familiar with the nature of unicode, but I'm not sure how all the pieces fit together. I have an error when displaying specific instances in the admin page.
Caught UnicodeEncodeError while rendering: 'ascii' codec can't encode character u'\u2019' in position 29: ordinal not in range(128)
Here's my model:
class Proposal(models.Model): project = models.ForeignKey(Project) dateCreated = models.DateTimeField(editable=False) xml = models.TextField(max_length=1000000) def __str__(self): return str('Proposal for: %s' % self.project.name)
I've gone into my mysql database and verified that the DB, table, and column are all collated as utf8_unicode_ci, so I don't understand why the page is trying to render as ascii. Looking at various forums and docs, I see mention of the str and unicode functions, but they don't seem to have anything to do with this as the list of instances shows up fine in on the admin page. It's just showing the actual instance form that causes a problem.
Here's some example xml I pulled from phpmyadmin...
<?xml version="1.0" encoding="UTF-8"?> <proposal> <section title="OVERVIEW"> <section title="Introduction"> <text> This proposal is not in the system because it was completed as an agreement in Word previous to us getting this application up and running. Please refer to the attachments in this project for documentation or to see the agreement. </text> </section> </section> </proposal>
I've even tried to deliberately exclude the xml (which I can't do in the long run since I'd like it to be editable in the admin section), but I still get the same error, so I'm not even convinced the xml is even the problem. If the xml isn't the problem, I have no idea what else could be keeping this page from being displayed.
class ProposalAdmin(admin.ModelAdmin): exclude = ('xml',) admin.site.register(Project)
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
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.
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