mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #18269 -- Applied unicode_literals for Python 3 compatibility.
Thanks Vinay Sajip for the support of his django3 branch and Jannis Leidel for the review.
This commit is contained in:
parent
706fd9adc0
commit
4a103086d5
401 changed files with 6647 additions and 6157 deletions
|
@ -94,10 +94,11 @@ The ``ContentFile`` Class
|
|||
but unlike :class:`~django.core.files.File` it operates on string content,
|
||||
rather than an actual file. For example::
|
||||
|
||||
from __future__ import unicode_literals
|
||||
from django.core.files.base import ContentFile
|
||||
|
||||
f1 = ContentFile(b"my string content")
|
||||
f2 = ContentFile(u"my unicode content encoded as UTF-8".encode('UTF-8'))
|
||||
f2 = ContentFile("my unicode content encoded as UTF-8".encode('UTF-8'))
|
||||
|
||||
.. currentmodule:: django.core.files.images
|
||||
|
||||
|
|
|
@ -25,8 +25,6 @@ exception or returns the clean value::
|
|||
>>> f = forms.EmailField()
|
||||
>>> f.clean('foo@example.com')
|
||||
u'foo@example.com'
|
||||
>>> f.clean(u'foo@example.com')
|
||||
u'foo@example.com'
|
||||
>>> f.clean('invalid email address')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
|
|
|
@ -184,7 +184,7 @@ a look at Django's ``EmailField``::
|
|||
|
||||
class EmailField(CharField):
|
||||
default_error_messages = {
|
||||
'invalid': _(u'Enter a valid e-mail address.'),
|
||||
'invalid': _('Enter a valid e-mail address.'),
|
||||
}
|
||||
default_validators = [validators.validate_email]
|
||||
|
||||
|
@ -197,7 +197,7 @@ on field definition so::
|
|||
is equivalent to::
|
||||
|
||||
email = forms.CharField(validators=[validators.validate_email],
|
||||
error_messages={'invalid': _(u'Enter a valid e-mail address.')})
|
||||
error_messages={'invalid': _('Enter a valid e-mail address.')})
|
||||
|
||||
|
||||
Form field default cleaning
|
||||
|
|
|
@ -45,6 +45,28 @@ rendering or anywhere else -- you have two choices for encoding those strings.
|
|||
You can use Unicode strings, or you can use normal strings (sometimes called
|
||||
"bytestrings") that are encoded using UTF-8.
|
||||
|
||||
.. versionchanged:: 1.5
|
||||
|
||||
In Python 3, the logic is reversed, that is normal strings are Unicode, and
|
||||
when you want to specifically create a bytestring, you have to prefix the
|
||||
string with a 'b'. As we are doing in Django code from version 1.5,
|
||||
we recommend that you import ``unicode_literals`` from the __future__ library
|
||||
in your code. Then, when you specifically want to create a bytestring literal,
|
||||
prefix the string with 'b'.
|
||||
|
||||
Python 2 legacy::
|
||||
|
||||
my_string = "This is a bytestring"
|
||||
my_unicode = u"This is an Unicode string"
|
||||
|
||||
Python 2 with unicode literals or Python 3::
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
my_string = b"This is a bytestring"
|
||||
my_unicode = "This is an Unicode string"
|
||||
|
||||
|
||||
.. admonition:: Warning
|
||||
|
||||
A bytestring does not carry any information with it about its encoding.
|
||||
|
@ -182,7 +204,7 @@ An example might clarify things here::
|
|||
|
||||
>>> urlquote(u'Paris & Orléans')
|
||||
u'Paris%20%26%20Orl%C3%A9ans'
|
||||
>>> iri_to_uri(u'/favorites/François/%s' % urlquote(u'Paris & Orléans'))
|
||||
>>> iri_to_uri(u'/favorites/François/%s' % urlquote('Paris & Orléans'))
|
||||
'/favorites/Fran%C3%A7ois/Paris%20%26%20Orl%C3%A9ans'
|
||||
|
||||
If you look carefully, you can see that the portion that was generated by
|
||||
|
@ -268,7 +290,9 @@ You can pass either Unicode strings or UTF-8 bytestrings as arguments to
|
|||
``filter()`` methods and the like in the database API. The following two
|
||||
querysets are identical::
|
||||
|
||||
qs = People.objects.filter(name__contains=u'Å')
|
||||
from __future__ import unicode_literals
|
||||
|
||||
qs = People.objects.filter(name__contains='Å')
|
||||
qs = People.objects.filter(name__contains=b'\xc3\x85') # UTF-8 encoding of Å
|
||||
|
||||
Templates
|
||||
|
@ -276,9 +300,10 @@ Templates
|
|||
|
||||
You can use either Unicode or bytestrings when creating templates manually::
|
||||
|
||||
from django.template import Template
|
||||
t1 = Template(b'This is a bytestring template.')
|
||||
t2 = Template(u'This is a Unicode template.')
|
||||
from __future__ import unicode_literals
|
||||
from django.template import Template
|
||||
t1 = Template(b'This is a bytestring template.')
|
||||
t2 = Template('This is a Unicode template.')
|
||||
|
||||
But the common case is to read templates from the filesystem, and this creates
|
||||
a slight complication: not all filesystems store their data encoded as UTF-8.
|
||||
|
@ -316,14 +341,15 @@ characters.
|
|||
The following code example demonstrates that everything except email addresses
|
||||
can be non-ASCII::
|
||||
|
||||
from __future__ import unicode_literals
|
||||
from django.core.mail import EmailMessage
|
||||
|
||||
subject = u'My visit to Sør-Trøndelag'
|
||||
sender = u'Arnbjörg Ráðormsdóttir <arnbjorg@example.com>'
|
||||
subject = 'My visit to Sør-Trøndelag'
|
||||
sender = 'Arnbjörg Ráðormsdóttir <arnbjorg@example.com>'
|
||||
recipients = ['Fred <fred@example.com']
|
||||
body = u'...'
|
||||
body = '...'
|
||||
msg = EmailMessage(subject, body, sender, recipients)
|
||||
msg.attach(u"Une pièce jointe.pdf", "%PDF-1.4.%...", mimetype="application/pdf")
|
||||
msg.attach("Une pièce jointe.pdf", "%PDF-1.4.%...", mimetype="application/pdf")
|
||||
msg.send()
|
||||
|
||||
Form submission
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue