开发者

Django db encoding

I have a little problem with encoding. The data in db is ok, when I select the data in php its ok. Problem comes when I get the data and try to print it in the template, I get - Å port instead of Šport, etc.

Everything is set to utf-8 - in settings.py, meta tags in template, db table and I even have unicode method specified for the model, but nothing seems to work. I am getting pretty hopeless here...

Here is some code:

class Category_info(models.Model):
  开发者_JAVA百科objtree_label_id = models.AutoField(primary_key = True)
  node_id = models.IntegerField(unique = True)
  language_id = models.IntegerField()
  label = models.CharField(max_length = 255)
  type_id = models.IntegerField()

class Meta:
    db_table = 'objtree_labels'

def __unicode__(self):
    return self.label

I have even tried with return u"%s" % self.label.

Here is the view:

def categories_list(request):
  categories_list = Category.objects.filter(parent_id = 1, status = 1)
  paginator = Paginator(categories_list, 10)

try:
    page = int(request.GET.get('page', 1))
except ValueError:
    page = 1

try:
    categories = paginator.page(page)
except (EmptyPage, InvalidPage):
    categories = paginator.page(paginator.num_pages)

return render_to_response('categories_list.html', {'categories': categories})

Maybe I am just blind and/or stupid, but it just doesnt work. So any help is appreciated, thanks in advance.

Regards


It's definitely not Django issue. As far as I understood you try to introspect existing DB (I suppose it's MySQL because it looks like common problem after incorrect upgrade from 4.x to 5.x). You should find out necessary connect options and provide them via DATABASE_OPTIONS setting. Try something like this:

DATABASE_OPTIONS = {
    'use_unicode': True,
    'charset': 'utf8'
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜