如何自定义包含unicode的django表单标签?

时间:2021-02-16 19:21:11

I created a form contains unicode like this:

我创建了一个表单包含这样的unicode:

# -*- coding: utf-8 -*-
from django.db import models
from django.forms import ModelForm

class Member(models.Model):
    family_name = models.CharField(u"姓",max_length=200)
    given_name = models.CharField(max_length=200)
    gender=models.IntegerField()

I used '# -- coding: utf-8 --' prefix u"姓"

我用“#——编码:utf - 8——”前缀u“姓”

but it still raise this exception: SyntaxError: (unicode error) 'utf8' codec can't decode byte 0xd0 in position 0: invalid continuation byte

但它仍然引发了这个异常:SyntaxError: (unicode error)“utf8”编解码器在0位置无法解码字节0xd0:无效的延续字节

this bothered me for several days , I'll really appreciate for any help I can get.

这使我烦恼了好几天,我将非常感激我能得到的任何帮助。

1 个解决方案

#1


1  

I had this problem a few days ago. The problem is the encoding of your .py file - it's not UTF-8.

几天前我遇到了这个问题。问题是你的.py文件的编码——它不是UTF-8。

Open the file in a text editor like NotePad++ and check file's encoding (Shown in the bottom right hand corner). If not all characters are shown, go to "Encoding" -> "Encode In", and select an appropriate encoding. Perhaps "Big5" or "GBK" is appropriate for that character.

在记事本++之类的文本编辑器中打开文件,检查文件的编码(显示在右下角)。如果没有显示所有字符,请转到“编码”—>“编码”,并选择适当的编码。也许“Big5”或“GBK”适合这个角色。

Once all non-ACSII characters are shown correctly, goto "Encoding" -> "Convert to", and select "UTF-8". Save the file. The file is now encoded as UTF-8.

一旦所有非acsii字符被正确显示后,执行“编码”—>“转换为”,并选择“UTF-8”。保存文件。该文件现在被编码为UTF-8。

#1


1  

I had this problem a few days ago. The problem is the encoding of your .py file - it's not UTF-8.

几天前我遇到了这个问题。问题是你的.py文件的编码——它不是UTF-8。

Open the file in a text editor like NotePad++ and check file's encoding (Shown in the bottom right hand corner). If not all characters are shown, go to "Encoding" -> "Encode In", and select an appropriate encoding. Perhaps "Big5" or "GBK" is appropriate for that character.

在记事本++之类的文本编辑器中打开文件,检查文件的编码(显示在右下角)。如果没有显示所有字符,请转到“编码”—>“编码”,并选择适当的编码。也许“Big5”或“GBK”适合这个角色。

Once all non-ACSII characters are shown correctly, goto "Encoding" -> "Convert to", and select "UTF-8". Save the file. The file is now encoded as UTF-8.

一旦所有非acsii字符被正确显示后,执行“编码”—>“转换为”,并选择“UTF-8”。保存文件。该文件现在被编码为UTF-8。