views:

38

answers:

1

Hey,

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):
  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

A: 

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'
}
uptimebox
I tried your suggestion and unfortunately nothing has changed. Yes, its an existing database (Mysql 5.x for a looong time now), it wasnt upgraded though. Before I have changed database options to what you recommended, I used - "init_command": "SET NAMES 'utf8'". Could that be the problem? But on the other hand I am using an existing db...
realshadow
One more thing - when I will update the data in database trough django, the encoding is ok.
realshadow