summaryrefslogtreecommitdiff
path: root/docs/ref
diff options
context:
space:
mode:
Diffstat (limited to 'docs/ref')
-rw-r--r--docs/ref/files/file.txt3
-rw-r--r--docs/ref/forms/fields.txt2
-rw-r--r--docs/ref/forms/validation.txt4
-rw-r--r--docs/ref/unicode.txt44
4 files changed, 39 insertions, 14 deletions
diff --git a/docs/ref/files/file.txt b/docs/ref/files/file.txt
index 10108d1f4f..99547f1c9e 100644
--- a/docs/ref/files/file.txt
+++ b/docs/ref/files/file.txt
@@ -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
diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt
index d9bfbc5e45..486d49d796 100644
--- a/docs/ref/forms/fields.txt
+++ b/docs/ref/forms/fields.txt
@@ -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):
...
diff --git a/docs/ref/forms/validation.txt b/docs/ref/forms/validation.txt
index f1642148b5..f6cdfc8141 100644
--- a/docs/ref/forms/validation.txt
+++ b/docs/ref/forms/validation.txt
@@ -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
diff --git a/docs/ref/unicode.txt b/docs/ref/unicode.txt
index 46ce4138a4..85e48ae15d 100644
--- a/docs/ref/unicode.txt
+++ b/docs/ref/unicode.txt
@@ -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