2

I created a form contains unicode like this:

# -*- 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"姓"

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

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

Arthur
  • 35
  • 4

1 Answers1

1

I had this problem a few days ago. The problem is the encoding of your .py file - it's not 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.

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.

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
  • 1
    It's worked! Thank you very much!! Now I can continue my work!! – Arthur Dec 12 '15 at 07:05
  • My pleasure pal :) accept my answer for helping others ... good luck @Arthur you know to how say thanks [How to Say Thanks in an Answer](https://blog.stackoverflow.com/2011/01/how-to-say-thanks-in-an-answer) – Arash Hatami Dec 12 '15 at 08:45
  • 1
    hey, there might be a bunch of other reasons, depending on where the error has been thrown, all related to unicode and the environment executing the code. it's worth reading this (and maybe expanding your answer, @ArashHatami) http://stackoverflow.com/questions/31717911/pythons-handling-of-shell-strings – gru Dec 12 '15 at 09:47