2009-12-26 11:44:21 +08:00
|
|
|
============
|
|
|
|
Unicode data
|
|
|
|
============
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2018-10-25 08:01:19 +08:00
|
|
|
Django supports Unicode data everywhere.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
|
|
|
This document tells you what you need to know if you're writing applications
|
|
|
|
that use data or templates that are encoded in something other than ASCII.
|
|
|
|
|
|
|
|
Creating the database
|
|
|
|
=====================
|
|
|
|
|
|
|
|
Make sure your database is configured to be able to store arbitrary string
|
|
|
|
data. Normally, this means giving it an encoding of UTF-8 or UTF-16. If you use
|
|
|
|
a more restrictive encoding -- for example, latin1 (iso8859-1) -- you won't be
|
|
|
|
able to store certain characters in the database, and information will be lost.
|
|
|
|
|
2014-08-02 22:27:01 +08:00
|
|
|
* MySQL users, refer to the `MySQL manual`_ for details on how to set or alter
|
|
|
|
the database character set encoding.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2013-03-17 18:45:45 +08:00
|
|
|
* PostgreSQL users, refer to the `PostgreSQL manual`_ (section 22.3.2 in
|
|
|
|
PostgreSQL 9) for details on creating databases with the correct encoding.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2015-09-16 04:01:31 +08:00
|
|
|
* Oracle users, refer to the `Oracle manual`_ for details on how to set
|
|
|
|
(`section 2`_) or alter (`section 11`_) the database character set encoding.
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* SQLite users, there is nothing you need to do. SQLite always uses UTF-8
|
|
|
|
for internal encoding.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2016-09-30 20:58:59 +08:00
|
|
|
.. _MySQL manual: https://dev.mysql.com/doc/refman/en/charset-database.html
|
2019-03-30 09:49:44 +08:00
|
|
|
.. _PostgreSQL manual: https://www.postgresql.org/docs/current/multibyte.html
|
2019-02-07 16:59:47 +08:00
|
|
|
.. _Oracle manual: https://docs.oracle.com/en/database/oracle/oracle-database/18/nlspg/index.html
|
|
|
|
.. _section 2: https://docs.oracle.com/en/database/oracle/oracle-database/18/nlspg/choosing-character-set.html
|
|
|
|
.. _section 11: https://docs.oracle.com/en/database/oracle/oracle-database/18/nlspg/character-set-migration.html
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2017-01-21 05:04:05 +08:00
|
|
|
All of Django's database backends automatically convert strings into
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
the appropriate encoding for talking to the database. They also automatically
|
2017-01-21 05:04:05 +08:00
|
|
|
convert strings retrieved from the database into strings. You don't even need
|
|
|
|
to tell Django what encoding your database uses: that is handled transparently.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
|
|
|
For more, see the section "The database API" below.
|
|
|
|
|
|
|
|
General string handling
|
|
|
|
=======================
|
|
|
|
|
|
|
|
Whenever you use strings with Django -- e.g., in database lookups, template
|
|
|
|
rendering or anywhere else -- you have two choices for encoding those strings.
|
2017-01-21 05:04:05 +08:00
|
|
|
You can use normal strings or bytestrings (starting with a 'b').
|
2012-06-08 00:08:47 +08:00
|
|
|
|
2013-02-20 00:31:41 +08:00
|
|
|
.. warning::
|
2007-07-04 20:48:12 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
A bytestring does not carry any information with it about its encoding.
|
|
|
|
For that reason, we have to make an assumption, and Django assumes that all
|
|
|
|
bytestrings are in UTF-8.
|
|
|
|
|
|
|
|
If you pass a string to Django that has been encoded in some other format,
|
|
|
|
things will go wrong in interesting ways. Usually, Django will raise a
|
|
|
|
``UnicodeDecodeError`` at some point.
|
|
|
|
|
|
|
|
If your code only uses ASCII data, it's safe to use your normal strings,
|
|
|
|
passing them around at will, because ASCII is a subset of UTF-8.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Don't be fooled into thinking that if your :setting:`DEFAULT_CHARSET` setting is set
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
to something other than ``'utf-8'`` you can use that other encoding in your
|
2008-08-24 06:25:40 +08:00
|
|
|
bytestrings! :setting:`DEFAULT_CHARSET` only applies to the strings generated as
|
2011-04-02 00:10:22 +08:00
|
|
|
the result of template rendering (and email). Django will always assume UTF-8
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
encoding for internal bytestrings. The reason for this is that the
|
2008-08-24 06:25:40 +08:00
|
|
|
:setting:`DEFAULT_CHARSET` setting is not actually under your control (if you are the
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
application developer). It's under the control of the person installing and
|
|
|
|
using your application -- and if that person chooses a different setting, your
|
|
|
|
code must still continue to work. Ergo, it cannot rely on that setting.
|
|
|
|
|
|
|
|
In most cases when Django is dealing with strings, it will convert them to
|
2017-01-21 05:04:05 +08:00
|
|
|
strings before doing anything else. So, as a general rule, if you pass
|
|
|
|
in a bytestring, be prepared to receive a string back in the result.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
|
|
|
Translated strings
|
|
|
|
------------------
|
|
|
|
|
2017-01-21 05:04:05 +08:00
|
|
|
Aside from strings and bytestrings, there's a third type of string-like
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
object you may encounter when using Django. The framework's
|
|
|
|
internationalization features introduce the concept of a "lazy translation" --
|
|
|
|
a string that has been marked as translated but whose actual translation result
|
|
|
|
isn't determined until the object is used in a string. This feature is useful
|
|
|
|
in cases where the translation locale is unknown until the string is used, even
|
|
|
|
though the string might have originally been created when the code was first
|
|
|
|
imported.
|
|
|
|
|
|
|
|
Normally, you won't have to worry about lazy translations. Just be aware that
|
|
|
|
if you examine an object and it claims to be a
|
|
|
|
``django.utils.functional.__proxy__`` object, it is a lazy translation.
|
2017-01-19 00:51:29 +08:00
|
|
|
Calling ``str()`` with the lazy translation as the argument will generate a
|
2017-01-21 05:04:05 +08:00
|
|
|
string in the current locale.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
|
|
|
For more details about lazy translation objects, refer to the
|
2010-08-20 03:27:44 +08:00
|
|
|
:doc:`internationalization </topics/i18n/index>` documentation.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
|
|
|
Useful utility functions
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
Because some string operations come up again and again, Django ships with a few
|
2017-01-21 05:04:05 +08:00
|
|
|
useful functions that should make working with string and bytestring objects
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
a bit easier.
|
|
|
|
|
|
|
|
Conversion functions
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The ``django.utils.encoding`` module contains a few functions that are handy
|
2017-01-21 05:04:05 +08:00
|
|
|
for converting back and forth between strings and bytestrings.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2017-01-26 17:08:08 +08:00
|
|
|
* ``smart_str(s, encoding='utf-8', strings_only=False, errors='strict')``
|
2017-01-21 05:04:05 +08:00
|
|
|
converts its input to a string. The ``encoding`` parameter
|
2011-10-14 08:12:01 +08:00
|
|
|
specifies the input encoding. (For example, Django uses this internally
|
|
|
|
when processing form input data, which might not be UTF-8 encoded.) The
|
|
|
|
``strings_only`` parameter, if set to True, will result in Python
|
|
|
|
numbers, booleans and ``None`` not being converted to a string (they keep
|
|
|
|
their original types). The ``errors`` parameter takes any of the values
|
2017-01-19 00:51:29 +08:00
|
|
|
that are accepted by Python's ``str()`` function for its error
|
2011-10-14 08:12:01 +08:00
|
|
|
handling.
|
|
|
|
|
2017-01-26 17:08:08 +08:00
|
|
|
* ``force_str(s, encoding='utf-8', strings_only=False, errors='strict')`` is
|
|
|
|
identical to ``smart_str()`` in almost all cases. The difference is when the
|
|
|
|
first argument is a :ref:`lazy translation <lazy-translations>` instance.
|
|
|
|
While ``smart_str()`` preserves lazy translations, ``force_str()`` forces
|
|
|
|
those objects to a string (causing the translation to occur). Normally,
|
|
|
|
you'll want to use ``smart_str()``. However, ``force_str()`` is useful in
|
|
|
|
template tags and filters that absolutely *must* have a string to work with,
|
|
|
|
not just something that can be converted to a string.
|
2011-10-14 08:12:01 +08:00
|
|
|
|
2012-07-21 16:00:10 +08:00
|
|
|
* ``smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict')``
|
2017-01-26 17:08:08 +08:00
|
|
|
is essentially the opposite of ``smart_str()``. It forces the first
|
2011-10-14 08:12:01 +08:00
|
|
|
argument to a bytestring. The ``strings_only`` parameter has the same
|
2017-01-26 17:08:08 +08:00
|
|
|
behavior as for ``smart_str()`` and ``force_str()``. This is
|
2011-10-14 08:12:01 +08:00
|
|
|
slightly different semantics from Python's builtin ``str()`` function,
|
|
|
|
but the difference is needed in a few places within Django's internals.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2017-01-26 17:08:08 +08:00
|
|
|
Normally, you'll only need to use ``force_str()``. Call it as early as
|
2017-01-21 05:04:05 +08:00
|
|
|
possible on any input data that might be either a string or a bytestring, and
|
|
|
|
from then on, you can treat the result as always being a string.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2011-04-21 03:50:27 +08:00
|
|
|
.. _uri-and-iri-handling:
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
URI and IRI handling
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2020-04-15 19:11:13 +08:00
|
|
|
Web frameworks have to deal with URLs (which are a type of IRI). One
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
requirement of URLs is that they are encoded using only ASCII characters.
|
|
|
|
However, in an international environment, you might need to construct a
|
2020-04-15 19:11:13 +08:00
|
|
|
URL from an :rfc:`IRI <3987>` -- very loosely speaking, a :rfc:`URI <2396>`
|
|
|
|
that can contain Unicode characters. Use these functions for quoting and
|
|
|
|
converting an IRI to a URI:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2017-11-01 23:11:12 +08:00
|
|
|
* The :func:`django.utils.encoding.iri_to_uri()` function, which implements the
|
|
|
|
conversion from IRI to URI as required by :rfc:`3987#section-3.1`.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2017-11-01 23:11:12 +08:00
|
|
|
* The :func:`urllib.parse.quote` and :func:`urllib.parse.quote_plus`
|
|
|
|
functions from Python's standard library.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
|
|
|
These two groups of functions have slightly different purposes, and it's
|
2017-11-01 23:11:12 +08:00
|
|
|
important to keep them straight. Normally, you would use ``quote()`` on the
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
individual portions of the IRI or URI path so that any reserved characters
|
|
|
|
such as '&' or '%' are correctly encoded. Then, you apply ``iri_to_uri()`` to
|
|
|
|
the full IRI and it converts any non-ASCII characters to the correct encoded
|
|
|
|
values.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
Technically, it isn't correct to say that ``iri_to_uri()`` implements the
|
|
|
|
full algorithm in the IRI specification. It doesn't (yet) perform the
|
|
|
|
international domain name encoding portion of the algorithm.
|
|
|
|
|
|
|
|
The ``iri_to_uri()`` function will not change ASCII characters that are
|
|
|
|
otherwise permitted in a URL. So, for example, the character '%' is not
|
|
|
|
further encoded when passed to ``iri_to_uri()``. This means you can pass a
|
|
|
|
full URL to this function and it will not mess up the query string or anything
|
|
|
|
like that.
|
|
|
|
|
|
|
|
An example might clarify things here::
|
|
|
|
|
2017-11-01 23:11:12 +08:00
|
|
|
>>> from urllib.parse import quote
|
|
|
|
>>> from django.utils.encoding import iri_to_uri
|
|
|
|
>>> quote('Paris & Orléans')
|
2014-03-23 04:30:49 +08:00
|
|
|
'Paris%20%26%20Orl%C3%A9ans'
|
2017-11-01 23:11:12 +08:00
|
|
|
>>> iri_to_uri('/favorites/François/%s' % quote('Paris & Orléans'))
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
'/favorites/Fran%C3%A7ois/Paris%20%26%20Orl%C3%A9ans'
|
|
|
|
|
|
|
|
If you look carefully, you can see that the portion that was generated by
|
2017-11-01 23:11:12 +08:00
|
|
|
``quote()`` in the second example was not double-quoted when passed to
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
``iri_to_uri()``. This is a very important and useful feature. It means that
|
|
|
|
you can construct your IRI without worrying about whether it contains
|
|
|
|
non-ASCII characters and then, right at the end, call ``iri_to_uri()`` on the
|
|
|
|
result.
|
|
|
|
|
2014-07-22 20:25:22 +08:00
|
|
|
Similarly, Django provides :func:`django.utils.encoding.uri_to_iri()` which
|
|
|
|
implements the conversion from URI to IRI as per :rfc:`3987#section-3.2`.
|
|
|
|
|
|
|
|
An example to demonstrate::
|
|
|
|
|
2017-11-01 23:11:12 +08:00
|
|
|
>>> from django.utils.encoding import uri_to_iri
|
2014-07-22 20:25:22 +08:00
|
|
|
>>> uri_to_iri('/%E2%99%A5%E2%99%A5/?utf8=%E2%9C%93')
|
|
|
|
'/♥♥/?utf8=✓'
|
2017-02-07 21:55:44 +08:00
|
|
|
>>> uri_to_iri('%A9hello%3Fworld')
|
|
|
|
'%A9hello%3Fworld'
|
2014-07-22 20:25:22 +08:00
|
|
|
|
2017-02-07 21:55:44 +08:00
|
|
|
In the first example, the UTF-8 characters are unquoted. In the second, the
|
|
|
|
percent-encodings remain unchanged because they lie outside the valid UTF-8
|
|
|
|
range or represent a reserved character.
|
2014-07-22 20:25:22 +08:00
|
|
|
|
|
|
|
Both ``iri_to_uri()`` and ``uri_to_iri()`` functions are idempotent, which means the
|
|
|
|
following is always true::
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2015-03-12 10:43:56 +08:00
|
|
|
iri_to_uri(iri_to_uri(some_string)) == iri_to_uri(some_string)
|
|
|
|
uri_to_iri(uri_to_iri(some_string)) == uri_to_iri(some_string)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2014-07-22 20:25:22 +08:00
|
|
|
So you can safely call it multiple times on the same URI/IRI without risking
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
double-quoting problems.
|
|
|
|
|
|
|
|
Models
|
|
|
|
======
|
|
|
|
|
2017-01-21 05:04:05 +08:00
|
|
|
Because all strings are returned from the database as ``str`` objects, model
|
2016-01-11 00:48:16 +08:00
|
|
|
fields that are character based (CharField, TextField, URLField, etc.) will
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
contain Unicode values when Django retrieves data from the database. This
|
|
|
|
is *always* the case, even if the data could fit into an ASCII bytestring.
|
|
|
|
|
|
|
|
You can pass in bytestrings when creating a model or populating a field, and
|
2017-01-21 05:04:05 +08:00
|
|
|
Django will convert it to strings when it needs to.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
|
|
|
Taking care in ``get_absolute_url()``
|
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
URLs can only contain ASCII characters. If you're constructing a URL from
|
|
|
|
pieces of data that might be non-ASCII, be careful to encode the results in a
|
2015-12-30 23:51:16 +08:00
|
|
|
way that is suitable for a URL. The :func:`~django.urls.reverse` function
|
|
|
|
handles this for you automatically.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2012-11-20 20:15:16 +08:00
|
|
|
If you're constructing a URL manually (i.e., *not* using the ``reverse()``
|
|
|
|
function), you'll need to take care of the encoding yourself. In this case,
|
2017-11-01 23:11:12 +08:00
|
|
|
use the ``iri_to_uri()`` and ``quote()`` functions that were documented
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
above_. For example::
|
|
|
|
|
2017-11-01 23:11:12 +08:00
|
|
|
from urllib.parse import quote
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.encoding import iri_to_uri
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
2017-11-01 23:11:12 +08:00
|
|
|
url = '/person/%s/?x=0&y=0' % quote(self.location)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return iri_to_uri(url)
|
|
|
|
|
|
|
|
This function returns a correctly encoded URL even if ``self.location`` is
|
|
|
|
something like "Jack visited Paris & Orléans". (In fact, the ``iri_to_uri()``
|
|
|
|
call isn't strictly necessary in the above example, because all the
|
|
|
|
non-ASCII characters would have been removed in quoting in the first line.)
|
|
|
|
|
2007-07-04 20:48:12 +08:00
|
|
|
.. _above: `URI and IRI handling`_
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
|
|
|
Templates
|
|
|
|
=========
|
|
|
|
|
2018-10-03 16:05:17 +08:00
|
|
|
Use strings when creating templates manually::
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
from django.template import Template
|
2017-01-21 05:04:05 +08:00
|
|
|
t2 = Template('This is a string template.')
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2018-10-03 11:45:19 +08:00
|
|
|
But the common case is to read templates from the filesystem. If your template
|
|
|
|
files are not stored with a UTF-8 encoding, adjust the :setting:`TEMPLATES`
|
|
|
|
setting. The built-in :py:mod:`~django.template.backends.django` backend
|
|
|
|
provides the ``'file_charset'`` option to change the encoding used to read
|
|
|
|
files from disk.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
The :setting:`DEFAULT_CHARSET` setting controls the encoding of rendered templates.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
This is set to UTF-8 by default.
|
|
|
|
|
|
|
|
Template tags and filters
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
A couple of tips to remember when writing your own template tags and filters:
|
|
|
|
|
2017-01-21 05:04:05 +08:00
|
|
|
* Always return strings from a template tag's ``render()`` method
|
2011-10-14 08:12:01 +08:00
|
|
|
and from template filters.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2017-01-26 17:08:08 +08:00
|
|
|
* Use ``force_str()`` in preference to ``smart_str()`` in these
|
2011-10-14 08:12:01 +08:00
|
|
|
places. Tag rendering and filter calls occur as the template is being
|
|
|
|
rendered, so there is no advantage to postponing the conversion of lazy
|
2017-01-21 05:04:05 +08:00
|
|
|
translation objects into strings. It's easier to work solely with
|
2011-10-14 08:12:01 +08:00
|
|
|
strings at that point.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2015-11-07 22:35:07 +08:00
|
|
|
.. _unicode-files:
|
|
|
|
|
|
|
|
Files
|
|
|
|
=====
|
|
|
|
|
|
|
|
If you intend to allow users to upload files, you must ensure that the
|
|
|
|
environment used to run Django is configured to work with non-ASCII file names.
|
|
|
|
If your environment isn't configured correctly, you'll encounter
|
|
|
|
``UnicodeEncodeError`` exceptions when saving files with file names that
|
|
|
|
contain non-ASCII characters.
|
|
|
|
|
|
|
|
Filesystem support for UTF-8 file names varies and might depend on the
|
|
|
|
environment. Check your current configuration in an interactive Python shell by
|
|
|
|
running::
|
|
|
|
|
|
|
|
import sys
|
|
|
|
sys.getfilesystemencoding()
|
|
|
|
|
|
|
|
This should output "UTF-8".
|
|
|
|
|
|
|
|
The ``LANG`` environment variable is responsible for setting the expected
|
|
|
|
encoding on Unix platforms. Consult the documentation for your operating system
|
|
|
|
and application server for the appropriate syntax and location to set this
|
|
|
|
variable.
|
|
|
|
|
|
|
|
In your development environment, you might need to add a setting to your
|
|
|
|
``~.bashrc`` analogous to:::
|
|
|
|
|
|
|
|
export LANG="en_US.UTF-8"
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Form submission
|
|
|
|
===============
|
|
|
|
|
|
|
|
HTML form submission is a tricky area. There's no guarantee that the
|
|
|
|
submission will include encoding information, which means the framework might
|
|
|
|
have to guess at the encoding of submitted data.
|
|
|
|
|
|
|
|
Django adopts a "lazy" approach to decoding form data. The data in an
|
|
|
|
``HttpRequest`` object is only decoded when you access it. In fact, most of
|
|
|
|
the data is not decoded at all. Only the ``HttpRequest.GET`` and
|
|
|
|
``HttpRequest.POST`` data structures have any decoding applied to them. Those
|
|
|
|
two fields will return their members as Unicode data. All other attributes and
|
|
|
|
methods of ``HttpRequest`` return data exactly as it was submitted by the
|
|
|
|
client.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
By default, the :setting:`DEFAULT_CHARSET` setting is used as the assumed encoding
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
for form data. If you need to change this for a particular form, you can set
|
2007-07-05 09:25:05 +08:00
|
|
|
the ``encoding`` attribute on an ``HttpRequest`` instance. For example::
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
|
|
|
def some_view(request):
|
|
|
|
# We know that the data must be encoded as KOI8-R (for some reason).
|
|
|
|
request.encoding = 'koi8-r'
|
|
|
|
...
|
|
|
|
|
|
|
|
You can even change the encoding after having accessed ``request.GET`` or
|
|
|
|
``request.POST``, and all subsequent accesses will use the new encoding.
|
|
|
|
|
|
|
|
Most developers won't need to worry about changing form encoding, but this is
|
|
|
|
a useful feature for applications that talk to legacy systems whose encoding
|
|
|
|
you cannot control.
|
|
|
|
|
|
|
|
Django does not decode the data of file uploads, because that data is normally
|
|
|
|
treated as collections of bytes, rather than strings. Any automatic decoding
|
|
|
|
there would alter the meaning of the stream of bytes.
|