Answer the question
In order to leave comments, you need to log in
Why does an error occur when saving a Cyrillic string in the Django admin fields?
Hello!
When saving any field through the admin panel in Cyrillic, an error pops up - UnicodeDecodeError ('ascii' codec can't decode byte 0xd0 in position 58: ordinal not in range(128)).
The database is MySQL, which works with Django through the MySQL Connector. OS - Mac OS X. Encoding in the database - UTF-8 everywhere. All files in UTF-8, at the very beginning of the files - # -*- coding: utf-8 -*-. When connecting to the database, the encoding is also indicated.
The code is the most trivial (almost the same as in the Django manuals):
models.py:
# -*- coding: utf-8 -*-
from django.db import models
class Lesson(models.Model):
link = models.CharField(max_length=200)
name = models.CharField(max_length=200)
pic_id = models.CharField(max_length=200)
# -*- coding: utf-8 -*-
from django.contrib import admin
from mytests.models import Lesson
admin.site.register(Lesson)
Answer the question
In order to leave comments, you need to log in
There was an error in the connector. With grief, I installed django.db.backends.mysql in half and now it works, everything is saved and works with a bang!
from django.utils.encoding import smart_unicode
class Lesson(models.Model):
....
def __unicode__(self):
smart_unicode(return self.name)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question