Django utf-8 和 django-mailer 的奇怪之处

Posted

技术标签:

【中文标题】Django utf-8 和 django-mailer 的奇怪之处【英文标题】:Django utf-8 and django-mailer strangeness 【发布时间】:2011-04-03 08:34:28 【问题描述】:

来自trunk http://github.com/jtauber/django-mailer/tree/master/docs/的最新django邮件

使用 Postgresql 8.4、sqlite3 测试

模板

 title 

forms.py

#-*- coding: utf-8 -*-

if "mailer" in settings.INSTALLED_APPS:
    from mailer import send_mail
else:
    from django.core.mail import send_mail


...
        body_txt = render_to_string(
            'mails/share_deal/email_body.txt',
            

                'title':u"éééààà",

            
        )

        send_mail( "",  body_txt , "foo@ff.ff",  ["bar@kl.fd"],  fail_silently=True)

错误

环境:

请求方法:POST 请求网址:http://localhost:8000/deals/share/ Django 版本:1.2 SVN-13596 Python版本:2.6.5 已安装的应用程序: ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.admin', 'django_extensions', 'django.contrib.markup', 'django.contrib.cmets', 'ajaxcmets', '邮递员', '个人资料', '标记', 'wmd', '核', '交易'] 已安装的中间件: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware')

Traceback:
File "/home/gregory/.virtualenvs/alpha/src/django/django/core/handlers/base.py" in get_response
  100.                     response = callback(request, *callback_args, **callback_kwargs)
File "/home/gregory/projects/casadeal/casadeal/core/decorators.py" in wrap
  12.         return f(request, *args, **kwargs)
File "/home/gregory/.virtualenvs/alpha/src/django/django/views/decorators/http.py" in inner
  37.             return func(request, *args, **kwargs)
File "/home/gregory/projects/casadeal/casadeal/deals/views.py" in share_deal
  22.             form.send_mail()
File "/home/gregory/projects/casadeal/casadeal/deals/forms.py" in send_mail
  89.             fail_silently=True)
File "/home/gregory/.virtualenvs/casadeal/src/django-mailer/mailer/__init__.py" in send_mail
  45.                  priority=priority).save()
File "/home/gregory/.virtualenvs/alpha/src/django/django/db/models/base.py" in save
  435.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/gregory/.virtualenvs/alpha/src/django/django/db/models/base.py" in save_base
  528.                     result = manager._insert(values, return_id=update_pk, using=using)
File "/home/gregory/.virtualenvs/alpha/src/django/django/db/models/manager.py" in _insert
  195.         return insert_query(self.model, values, **kwargs)
File "/home/gregory/.virtualenvs/alpha/src/django/django/db/models/query.py" in insert_query
  1479.     return query.get_compiler(using=using).execute_sql(return_id)
File "/home/gregory/.virtualenvs/alpha/src/django/django/db/models/sql/compiler.py" in execute_sql
  783.         cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/home/gregory/.virtualenvs/alpha/src/django/django/db/models/sql/compiler.py" in execute_sql
  727.         cursor.execute(sql, params)
File "/home/gregory/.virtualenvs/alpha/src/django/django/db/backends/util.py" in execute
  15.             return self.cursor.execute(sql, params)
File "/home/gregory/.virtualenvs/alpha/src/django/django/db/backends/postgresql_psycopg2/base.py" in execute
  44.             return self.cursor.execute(query, args)

Exception Type: DatabaseError at /deals/share/
Exception Value: ERREUR:  séquence d'octets invalide pour l'encodage « UTF8 » : 0xe9e9e9
HINT:  Cette erreur peut aussi survenir si la séquence d'octets ne correspond pas
au jeu de caractères attendu par le serveur, le jeu étant contrôlé par
« client_encoding ».

Basically it says that the encoding was not valid for UTF8 and contained   0xe9e9e9

这看起来很奇怪,

关于它可能来自哪里的任何提示?

【问题讨论】:

您是否检查过哪些字符作为电子邮件地址提交? 是的,如果我删除身体一切都很好 【参考方案1】:

U+00E9 是带有锐音的拉丁文小写字母 E。

这可能是问题的根源(不一定是原因):'title':u"éééààà",

原因可能类似于your_title.encode('latin1').decode('utf8')(当然不是一步)。

【讨论】:

以上是关于Django utf-8 和 django-mailer 的奇怪之处的主要内容,如果未能解决你的问题,请参考以下文章

Python / Django 中的 Unicode 与 UTF-8 混淆?

Django utf-8报错

Django utf-8报错

在 Django 中创建 UTF-8 JsonResponse

django rest_framework中将json输出字符强制为utf-8编码

如何将 utf-8 数据从 Django 传递到 javascript