0

request.POST['item']Django を使用して、フォーム ( ) を介した送信を、その行のデータベースに既にあるものと比較するビューを作成しようとしています。

私は を使用していますが、それによってオブジェクトEntry.objects.values_list('item', flat=True)のリストが得られ、比較が失敗します。unicode

なしで、これらのオブジェクトを純粋なリストとして取得するにはどうすればよいunicodeですか?

4

1 に答える 1

1

To create a unicode object, you can use

from settings import DEFAULT_CHARSET
s = unicode(request.POST['item'], request.encoding or DEFAULT_CHARSET)

Note that items inside request.POST should already be of type unicode, hence no conversion should be required.

In [1]: a = u'Täöüß'

In [2]: a
Out[2]: u'T\xe4\xf6\xfc\xdf'

In [3]: type(a)
Out[3]: unicode

The following will work as unicode does not have to do anything:

In [4]: unicode(a)
Out[4]: u'T\xe4\xf6\xfc\xdf'

Forcing unicode to decode from utf-8 encoding, however, must fail as a is not an 8-bit string but a unicode variable:

In [5]: unicode(a, 'utf-8')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
----> 1 unicode(a, 'utf-8')

TypeError: decoding Unicode is not supported

You can use .encode(encoding) on the unicode object to create 8-bit string versions:

In [6]: utf8 = a.encode('utf-8')

In [7]: latin1 = a.encode('latin-1')

In [8]: latin1
Out[8]: 'T\xe4\xf6\xfc\xdf'

In [9]: utf8
Out[9]: 'T\xc3\xa4\xc3\xb6\xc3\xbc\xc3\x9f'

Note that the resulting strings are of type str and not of type unicode.

If you are given str-typed data and know the corresponding encoding, you can create unicode objects like this:

In [10]: b = unicode(latin1)
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
----> 1 b = unicode(latin1)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 1:
 ordinal not in range(128)

This has to fail as no encoding is given which means a default encoding of ascii is assumed (which, of course, cannot handle 0xe4).

In [11]: b = unicode(latin1, 'utf-8')
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
----> 1 b = unicode(latin1, 'utf-8')

UnicodeDecodeError: 'utf8' codec can't decode byte 0xe4 in position 1:
 invalid continuation byte

Using a wrong encoding, in this case trying to decode from utf-8 when working on a latin-1 string, will also raise a UnicodeDecodeError.

In [12]: b = unicode(latin1, 'latin-1')

Using the right encoding will allow to create a unicode object for the given str instance. b is equal to a:

In [13]: b == a
Out[13]: True

Using this information you should be able to create unicode instances wherever you need them and thus compare request.POST data with values from your database.

More information on Unicode handling in Python is available here http://docs.python.org/howto/unicode.html

Django's documentation on Unicode data can be found here https://docs.djangoproject.com/en/dev/ref/unicode/

Hope that helps.

于 2012-04-14T06:13:38.280 に答える