添加新项目时django admin编码错误

时间:2016-07-08 22:12:22

标签: django encoding admin

请,我是django的新用户,我不明白我的模型有什么问题。 我已经激活了管理员,并创建了一个新模型:

class Strumenti(models.Model):
    cod_stru = models.AutoField(db_column='cod_stru', primary_key=True)
    sht_name = models.CharField(db_column='sht_name', max_length=15, blank=True, null=True)
    descr = models.CharField(db_column='descr', max_length=45, blank=True, null=True)
    isin = models.CharField(db_column='isin', max_length=16)
    tipo = models.CharField(db_column='tipo', max_length=1)
    mktcode = models.ForeignKey(Mercati, on_delete=models.CASCADE, default=1)
    ccy1 = models.ForeignKey(Valute, related_name='ccyone', on_delete=models.CASCADE)
    ccy2 = models.ForeignKey(Valute, related_name='ccytwo', on_delete=models.CASCADE)
    beta = models.DecimalField(db_column='beta', max_digits=2, decimal_places=2, blank=True, null=True)
    system = models.NullBooleanField(db_column='system', default=False, null=True)
    template = models.NullBooleanField(db_column='template', default=False, null=True)
    tenor = models.CharField(db_column='tenor', max_length=4, blank=True, null=True)
    divy = models.DecimalField(db_column='divy', max_digits=5, decimal_places=5, blank=True, null=True)
    fxpair = models.CharField(db_column='fxpair', max_length=6, blank=True, null=True)
    cod_emitt = models.ForeignKey(Emittenti, on_delete=models.CASCADE, null=True)
    idx_code = models.ForeignKey(Indexdef, on_delete=models.CASCADE, null=True)
    vol_type = models.IntegerField(db_column='vol_type', blank=True, null=True)

def __unicode__(self):
        return u'%s - %s - %s - %s' % (self.cod_stru, self.sht_name, self.descr, self.tipo)

当我尝试添加新记录时,我收到此错误:

**'ascii' codec can't encode character u'\xe3' in position 14: ordinal not in range(128)**

我正在使用Django 1.9,python 2.7,Mysql(utf8_unicode_ci)

下面是模板渲染过程中的错误

In template /usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/admin/change_form.html, error at line 33
ascii
23  {% endblock %}
24  {% endif %}
25  
26  {% block content %}<div id="content-main">
27  {% block object-tools %}
28  {% if change %}{% if not is_popup %}
29    <ul class="object-tools">
30      {% block object-tools-items %}
31      <li>
32          {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
33          <a href="{% add_preserved_filters history_url %}" class="historylink">{% trans "History" %}</a>
有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题。 我在“Valute”表中存储了“SãoTomé和Príncipe”的描述。

即使我不明白为什么我的.encode(uft-8)不能正常工作,但是像ãéí这样的字符会产生错误。 为什么我刚刚将字符串更改为“圣多美和普林西比”,现在看起来很好。