Django 1.9'ascii'编解码器不能编码字符u'\ xf1'

时间:2016-10-26 15:54:15

标签: python django python-2.7 django-models character-encoding

我在使用拉丁名字时遇到了同样的错误,我可以解决它在我的模型中定义 __unicode __ 函数,如下所示:

class MyModel(models.Model):
    # fields

    def __str__(self):
        return str(self.field) + " - " + self.field

    def __unicode__(self):
        return str(self.field) + " - " + self.field

但这一次,即使使用该功能,当我尝试在选择输入中显示它时,我也无法使其在其他模型中工作:

template.html

<div class="form-group">
  <label for="input_especialidad">Compañero</label>
  {{ puntual_form.employee }} <!-- the error is detected here -->
  <div id="help-error" class="hidden">
    <p class="help-block">Este campo es obligatorio.</p>
  </div>
  <div id="help-info">
    <p class="help-block">Los nombres empiezan por su apellido seguido por los nombres.</p>
    <p class="help-block">Tambien puedes teclear el nombre de tu compañero para buscarlo.</p>
  </div>
</div>


forms.py

class form_Nom_puntual(forms.ModelForm):
    class Meta:
        model = Nom_puntual
        fields = ['employee']
        widgets = {
            'employee': forms.Select(attrs={'class': 'form-control', 'id': 'puntual_employee'}),
        }


models.py

class Employees(models.Model):
    num_employee = models.PositiveIntegerField(unique=True, null=True)
    name = models.CharField(max_length=255)

    class Meta:
        ordering = ['name']

    def __str__(self):
        return str(self.name)

    def __unicode__(self):
        return str(self.name)

class Nom_puntual(models.Model):
    employee = models.OneToOneField(Employees, on_delete=models.PROTECT)
    ip_nominator = models.GenericIPAddressField()


回溯

Exception Type: UnicodeEncodeError
Exception Value: 'ascii' codec can't encode character u'\xf1' in position 4: ordinal not in range(128)
Exception Location: /home/rortega/smce/votaciones/models.py in __str__, line 12

The string that could not be encoded/decoded was: Aguiñaga F

我知道问题是ñ字符,但我的数据库的字符集 utf8 和整理 utf8_unicode_ci

0 个答案:

没有答案