Django 数据库编码

发布于 2024-08-31 18:58:19 字数 1183 浏览 4 评论 0原文

我在编码方面遇到了一些问题。 db中的数据是好的,当我选择php中的数据时就可以了。当我获取数据并尝试在模板中打印它时,问题就来了,我得到 - Å port 而不是 Šport 等。

所有内容都设置为 utf-8 - 在 settings.py 中,模板中的元标记,数据库表,甚至我什至为模型指定了 unicode 方法,但似乎没有任何作用。我在这里变得非常绝望......

这是一些代码:

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

我什至尝试过 return u"%s" % self.label。

这是观点:

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

也许我只是盲目和/或愚蠢,但这就是行不通。因此,感谢任何帮助,提前致谢。

问候

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜妞爱困 2024-09-07 18:58:19

这绝对不是 Django 的问题。据我了解,您尝试内省现有数据库(我认为它是 MySQL,因为它看起来像是从 4.​​x 错误升级到 5.x 后的常见问题)。您应该找到必要的连接选项并通过 DATABASE_OPTIONS 设置提供它们。尝试这样的事情:

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

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'
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文