简体   繁体   English

在Django中创建一个包含unicode的选择表单

[英]create a choice form in django that contains unicode

i have this choice form : 我有这个选择表:

class rqtime(forms.ChoiceField):

    def __init__(self, *args, **kwargs):
        super(rqtime, self).__init__(*args, **kwargs)
        self.required = True
        self.initial = True    
        self.choices=(('1','my_unicode'),('2','soon'),('3','1day'),('4','1week'),('5','3week'))
        global mydic
        mydic=dict(self.choices)

    def clean(self, value):
        return mydic[value]

i used from __future__ import unicode_literals and -*-coding:utf-8-*- but after runserver this is the raised error from 'my_unicode' : from __future__ import unicode_literals-*-coding:utf-8-*-但是在runserver之后,这是'my_unicode'引发的错误:

SyntaxError: Non-ASCII character '\xd9'

how can i decode an unicode in form django ! 我如何解码django形式的unicode!

You need to add u prefix before your unicode string, change 'my_unicode' into u'my_unicode' . 您需要在unicode字符串之前添加u前缀,将'my_unicode'更改为u'my_unicode'

self.choices=(('1',u'my_unicode'),('2','soon'),('3','1day'),('4','1week'),('5','3week'))

You can read more about Unicode Type in Python Doc . 您可以在Python Doc中阅读有关Unicode类型的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM