django special character handling

2019-09-13 03:11发布

I'm trying to understand how to deal correctly with special characters in Django / Python. I have added to my views.py and models.py the following encoding string:

# -*- coding: utf-8 -*-

But when the following cmd is called with a purchase order name set to "TestÄÜÖ" it crashes:

messages.add_message(request, messages.INFO, 'The purchase order "%s" has been successfully added to project "%s".' % (purchase_order, project.name))

The error thrown is the following:

File "..accounting/views.py", line 1100, in post_logic
    messages.add_message(request, messages.INFO, 'The purchase order "%s" has been successfully added to project "%s".' % (purchase_order, project.name))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 20: ordinal not in range(128)

The PurchaseOrder model looks like this.

class PurchaseOrder(models.Model):
    """
    purchase order assigned to a project
    """

    number = models.CharField(max_length=200)
    name = models.CharField(max_length=200, null=True, blank=True, default="")

    def __unicode__(self):
        return u'%s - %s' % (self.name, self.number)

The problem does not occur if I add u in front of the message string:

messages.add_message(request, messages.INFO, u'The purchase order "%s" has been successfully added to project "%s".' % (purchase_order, project.name))

But the docs say that in Django 1.5 (I'm using 1.5) a normal string should be a unicode string and there is no need for the u .

So I do not want to add to all my add_message calls an u, if the docs say it is not needed. Anybody can shed some light on this encoding topic?

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-13 03:49

You missed the from __future__ import unicode_literals that would make strings in Python2 act like Python3 unicode strings.

查看更多
登录 后发表回答