{"title":"Unicode data","version":"1.11","locale":"pt-br","docname":"ref/unicode","url":"/pt-br/1.11/ref/unicode/","canonical":"https://djangodocs.dev/pt-br/1.11/ref/unicode/","summary":"Django natively supports Unicode data everywhere. Providing your database can somehow store the data, you can safely pass around Unicode strings to templates,…","html":"<h1>Unicode data<a class=\"heading-anchor\" href=\"#unicode-data\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django natively supports Unicode data everywhere. Providing your database can\nsomehow store the data, you can safely pass around Unicode strings to\ntemplates, models and the database.</p>\n<p>This document tells you what you need to know if you’re writing applications\nthat use data or templates that are encoded in something other than ASCII.</p>\n<section id=\"creating-the-database\">\n<h2>Creating the database<a class=\"heading-anchor\" href=\"#creating-the-database\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Make sure your database is configured to be able to store arbitrary string\ndata. Normally, this means giving it an encoding of UTF-8 or UTF-16. If you use\na more restrictive encoding – for example, latin1 (iso8859-1) – you won’t be\nable to store certain characters in the database, and information will be lost.</p>\n<ul class=\"simple\">\n<li><p>MySQL users, refer to the <a class=\"reference external\" href=\"https://dev.mysql.com/doc/refman/en/charset-database.html\">MySQL manual</a> for details on how to set or alter\nthe database character set encoding.</p></li>\n<li><p>PostgreSQL users, refer to the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/static/multibyte.html\">PostgreSQL manual</a> (section 22.3.2 in\nPostgreSQL 9) for details on creating databases with the correct encoding.</p></li>\n<li><p>Oracle users, refer to the <a class=\"reference external\" href=\"https://docs.oracle.com/database/121/NLSPG/toc.htm\">Oracle manual</a> for details on how to set\n(<a class=\"reference external\" href=\"https://docs.oracle.com/database/121/NLSPG/ch2charset.htm#NLSPG002\">section 2</a>) or alter (<a class=\"reference external\" href=\"https://docs.oracle.com/database/121/NLSPG/ch11charsetmig.htm#NLSPG011\">section 11</a>) the database character set encoding.</p></li>\n<li><p>SQLite users, there is nothing you need to do. SQLite always uses UTF-8\nfor internal encoding.</p></li>\n</ul>\n<p>All of Django’s database backends automatically convert Unicode strings into\nthe appropriate encoding for talking to the database. They also automatically\nconvert strings retrieved from the database into Python Unicode strings. You\ndon’t even need to tell Django what encoding your database uses: that is\nhandled transparently.</p>\n<p>For more, see the section “The database API” below.</p>\n</section>\n<section id=\"general-string-handling\">\n<h2>General string handling<a class=\"heading-anchor\" href=\"#general-string-handling\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Whenever you use strings with Django – e.g., in database lookups, template\nrendering or anywhere else – you have two choices for encoding those strings.\nYou can use Unicode strings, or you can use normal strings (sometimes called\n“bytestrings”) that are encoded using UTF-8.</p>\n<p>In Python 3, the logic is reversed, that is normal strings are Unicode, and\nwhen you want to specifically create a bytestring, you have to prefix the\nstring with a ‘b’. As we are doing in Django code from version 1.5,\nwe recommend that you import <code class=\"docutils literal notranslate\"><span class=\"pre\">unicode_literals</span></code> from the __future__ library\nin your code. Then, when you specifically want to create a bytestring literal,\nprefix the string with ‘b’.</p>\n<p>Python 2 legacy:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">my_string</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;This is a bytestring&quot;</span>\n<span class=\"n\">my_unicode</span> <span class=\"o\">=</span> <span class=\"sa\">u</span><span class=\"s2\">&quot;This is an Unicode string&quot;</span>\n</code></pre></div>\n<p>Python 2 with unicode literals or Python 3:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">__future__</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">unicode_literals</span>\n\n<span class=\"n\">my_string</span> <span class=\"o\">=</span> <span class=\"sa\">b</span><span class=\"s2\">&quot;This is a bytestring&quot;</span>\n<span class=\"n\">my_unicode</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;This is an Unicode string&quot;</span>\n</code></pre></div>\n<p>See also <a class=\"reference internal\" href=\"/pt-br/1.11/topics/python3/\"><span class=\"doc\">Python 3 compatibility</span></a>.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Aviso</p>\n<p>A bytestring does not carry any information with it about its encoding.\nFor that reason, we have to make an assumption, and Django assumes that all\nbytestrings are in UTF-8.</p>\n<p>If you pass a string to Django that has been encoded in some other format,\nthings will go wrong in interesting ways. Usually, Django will raise a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">UnicodeDecodeError</span></code> at some point.</p>\n</aside>\n<p>If your code only uses ASCII data, it’s safe to use your normal strings,\npassing them around at will, because ASCII is a subset of UTF-8.</p>\n<p>Don’t be fooled into thinking that if your <a class=\"reference internal\" href=\"/pt-br/1.11/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_CHARSET</span></code></a> setting is set\nto something other than <code class=\"docutils literal notranslate\"><span class=\"pre\">'utf-8'</span></code> you can use that other encoding in your\nbytestrings! <a class=\"reference internal\" href=\"/pt-br/1.11/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_CHARSET</span></code></a> only applies to the strings generated as\nthe result of template rendering (and email). Django will always assume UTF-8\nencoding for internal bytestrings. The reason for this is that the\n<a class=\"reference internal\" href=\"/pt-br/1.11/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_CHARSET</span></code></a> setting is not actually under your control (if you are the\napplication developer). It’s under the control of the person installing and\nusing your application – and if that person chooses a different setting, your\ncode must still continue to work. Ergo, it cannot rely on that setting.</p>\n<p>In most cases when Django is dealing with strings, it will convert them to\nUnicode strings before doing anything else. So, as a general rule, if you pass\nin a bytestring, be prepared to receive a Unicode string back in the result.</p>\n<section id=\"translated-strings\">\n<h3>Translated strings<a class=\"heading-anchor\" href=\"#translated-strings\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Aside from Unicode strings and bytestrings, there’s a third type of string-like\nobject you may encounter when using Django. The framework’s\ninternationalization features introduce the concept of a “lazy translation” –\na string that has been marked as translated but whose actual translation result\nisn’t determined until the object is used in a string. This feature is useful\nin cases where the translation locale is unknown until the string is used, even\nthough the string might have originally been created when the code was first\nimported.</p>\n<p>Normally, you won’t have to worry about lazy translations. Just be aware that\nif you examine an object and it claims to be a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.functional.__proxy__</span></code> object, it is a lazy translation.\nCalling <code class=\"docutils literal notranslate\"><span class=\"pre\">unicode()</span></code> with the lazy translation as the argument will generate a\nUnicode string in the current locale.</p>\n<p>For more details about lazy translation objects, refer to the\n<a class=\"reference internal\" href=\"/pt-br/1.11/topics/i18n/\"><span class=\"doc\">internationalization</span></a> documentation.</p>\n</section>\n<section id=\"useful-utility-functions\">\n<h3>Useful utility functions<a class=\"heading-anchor\" href=\"#useful-utility-functions\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Because some string operations come up again and again, Django ships with a few\nuseful functions that should make working with Unicode and bytestring objects\na bit easier.</p>\n<section id=\"conversion-functions\">\n<h4>Conversion functions<a class=\"heading-anchor\" href=\"#conversion-functions\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.encoding</span></code> module contains a few functions that are handy\nfor converting back and forth between Unicode and bytestrings.</p>\n<ul>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">smart_text(s,</span> <span class=\"pre\">encoding='utf-8',</span> <span class=\"pre\">strings_only=False,</span> <span class=\"pre\">errors='strict')</span></code>\nconverts its input to a Unicode string. The <code class=\"docutils literal notranslate\"><span class=\"pre\">encoding</span></code> parameter\nspecifies the input encoding. (For example, Django uses this internally\nwhen processing form input data, which might not be UTF-8 encoded.) The\n<code class=\"docutils literal notranslate\"><span class=\"pre\">strings_only</span></code> parameter, if set to True, will result in Python\nnumbers, booleans and <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> not being converted to a string (they keep\ntheir original types). The <code class=\"docutils literal notranslate\"><span class=\"pre\">errors</span></code> parameter takes any of the values\nthat are accepted by Python’s <code class=\"docutils literal notranslate\"><span class=\"pre\">unicode()</span></code> function for its error\nhandling.</p>\n<p>If you pass <code class=\"docutils literal notranslate\"><span class=\"pre\">smart_text()</span></code> an object that has a <code class=\"docutils literal notranslate\"><span class=\"pre\">__unicode__</span></code>\nmethod, it will use that method to do the conversion.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">force_text(s,</span> <span class=\"pre\">encoding='utf-8',</span> <span class=\"pre\">strings_only=False,</span>\n<span class=\"pre\">errors='strict')</span></code> is identical to <code class=\"docutils literal notranslate\"><span class=\"pre\">smart_text()</span></code> in almost all\ncases. The difference is when the first argument is a <a class=\"reference internal\" href=\"/pt-br/1.11/topics/i18n/translation/#lazy-translations\"><span class=\"std std-ref\">lazy\ntranslation</span></a> instance. While <code class=\"docutils literal notranslate\"><span class=\"pre\">smart_text()</span></code>\npreserves lazy translations, <code class=\"docutils literal notranslate\"><span class=\"pre\">force_text()</span></code> forces those objects to a\nUnicode string (causing the translation to occur). Normally, you’ll want\nto use <code class=\"docutils literal notranslate\"><span class=\"pre\">smart_text()</span></code>. However, <code class=\"docutils literal notranslate\"><span class=\"pre\">force_text()</span></code> is useful in\ntemplate tags and filters that absolutely <em>must</em> have a string to work\nwith, not just something that can be converted to a string.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">smart_bytes(s,</span> <span class=\"pre\">encoding='utf-8',</span> <span class=\"pre\">strings_only=False,</span> <span class=\"pre\">errors='strict')</span></code>\nis essentially the opposite of <code class=\"docutils literal notranslate\"><span class=\"pre\">smart_text()</span></code>. It forces the first\nargument to a bytestring. The <code class=\"docutils literal notranslate\"><span class=\"pre\">strings_only</span></code> parameter has the same\nbehavior as for <code class=\"docutils literal notranslate\"><span class=\"pre\">smart_text()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">force_text()</span></code>. This is\nslightly different semantics from Python’s builtin <code class=\"docutils literal notranslate\"><span class=\"pre\">str()</span></code> function,\nbut the difference is needed in a few places within Django’s internals.</p></li>\n</ul>\n<p>Normally, you’ll only need to use <code class=\"docutils literal notranslate\"><span class=\"pre\">force_text()</span></code>. Call it as early as\npossible on any input data that might be either Unicode or a bytestring, and\nfrom then on, you can treat the result as always being Unicode.</p>\n</section>\n<section id=\"uri-and-iri-handling\">\n<span id=\"id1\"></span><h4>URI and IRI handling<a class=\"heading-anchor\" href=\"#uri-and-iri-handling\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Web frameworks have to deal with URLs (which are a type of <a class=\"reference external\" href=\"https://www.ietf.org/rfc/rfc3987.txt\">IRI</a>). One\nrequirement of URLs is that they are encoded using only ASCII characters.\nHowever, in an international environment, you might need to construct a\nURL from an <a class=\"reference external\" href=\"https://www.ietf.org/rfc/rfc3987.txt\">IRI</a> – very loosely speaking, a <a class=\"reference external\" href=\"https://www.ietf.org/rfc/rfc2396.txt\">URI</a> that can contain Unicode\ncharacters. Quoting and converting an IRI to URI can be a little tricky, so\nDjango provides some assistance.</p>\n<ul class=\"simple\">\n<li><p>The function <a class=\"reference internal\" href=\"/pt-br/1.11/ref/utils/#django.utils.encoding.iri_to_uri\" title=\"django.utils.encoding.iri_to_uri\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.utils.encoding.iri_to_uri()</span></code></a> implements the\nconversion from IRI to URI as required by the specification (<span class=\"target\" id=\"index-0\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc3987.html#section-3.1\"><strong>RFC 3987 Section 3.1</strong></a>).</p></li>\n<li><p>The functions <a class=\"reference internal\" href=\"/pt-br/1.11/ref/utils/#django.utils.http.urlquote\" title=\"django.utils.http.urlquote\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.utils.http.urlquote()</span></code></a> and\n<a class=\"reference internal\" href=\"/pt-br/1.11/ref/utils/#django.utils.http.urlquote_plus\" title=\"django.utils.http.urlquote_plus\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.utils.http.urlquote_plus()</span></code></a> are versions of Python’s standard\n<code class=\"docutils literal notranslate\"><span class=\"pre\">urllib.quote()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">urllib.quote_plus()</span></code> that work with non-ASCII\ncharacters. (The data is converted to UTF-8 prior to encoding.)</p></li>\n</ul>\n<p>These two groups of functions have slightly different purposes, and it’s\nimportant to keep them straight. Normally, you would use <code class=\"docutils literal notranslate\"><span class=\"pre\">urlquote()</span></code> on the\nindividual portions of the IRI or URI path so that any reserved characters\nsuch as ‘&amp;’ or ‘%’ are correctly encoded. Then, you apply <code class=\"docutils literal notranslate\"><span class=\"pre\">iri_to_uri()</span></code> to\nthe full IRI and it converts any non-ASCII characters to the correct encoded\nvalues.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>Technically, it isn’t correct to say that <code class=\"docutils literal notranslate\"><span class=\"pre\">iri_to_uri()</span></code> implements the\nfull algorithm in the IRI specification. It doesn’t (yet) perform the\ninternational domain name encoding portion of the algorithm.</p>\n</aside>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">iri_to_uri()</span></code> function will not change ASCII characters that are\notherwise permitted in a URL. So, for example, the character ‘%’ is not\nfurther encoded when passed to <code class=\"docutils literal notranslate\"><span class=\"pre\">iri_to_uri()</span></code>. This means you can pass a\nfull URL to this function and it will not mess up the query string or anything\nlike that.</p>\n<p>An example might clarify things here:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">urlquote</span><span class=\"p\">(</span><span class=\"s1\">&#39;Paris &amp; Orléans&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">&#39;Paris%20%26%20Orl%C3%A9ans&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">iri_to_uri</span><span class=\"p\">(</span><span class=\"s1\">&#39;/favorites/François/</span><span class=\"si\">%s</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span> <span class=\"n\">urlquote</span><span class=\"p\">(</span><span class=\"s1\">&#39;Paris &amp; Orléans&#39;</span><span class=\"p\">))</span>\n<span class=\"go\">&#39;/favorites/Fran%C3%A7ois/Paris%20%26%20Orl%C3%A9ans&#39;</span>\n</code></pre></div>\n<p>If you look carefully, you can see that the portion that was generated by\n<code class=\"docutils literal notranslate\"><span class=\"pre\">urlquote()</span></code> in the second example was not double-quoted when passed to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">iri_to_uri()</span></code>. This is a very important and useful feature. It means that\nyou can construct your IRI without worrying about whether it contains\nnon-ASCII characters and then, right at the end, call <code class=\"docutils literal notranslate\"><span class=\"pre\">iri_to_uri()</span></code> on the\nresult.</p>\n<p>Similarly, Django provides <a class=\"reference internal\" href=\"/pt-br/1.11/ref/utils/#django.utils.encoding.uri_to_iri\" title=\"django.utils.encoding.uri_to_iri\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.utils.encoding.uri_to_iri()</span></code></a> which\nimplements the conversion from URI to IRI as per <span class=\"target\" id=\"index-1\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc3987.html#section-3.2\"><strong>RFC 3987 Section 3.2</strong></a>.\nIt decodes all percent-encodings except those that don’t represent a valid\nUTF-8 sequence.</p>\n<p>An example to demonstrate:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">uri_to_iri</span><span class=\"p\">(</span><span class=\"s1\">&#39;/</span><span class=\"si\">%E</span><span class=\"s1\">2</span><span class=\"si\">%99%</span><span class=\"s1\">A5</span><span class=\"si\">%E</span><span class=\"s1\">2</span><span class=\"si\">%99%</span><span class=\"s1\">A5/?utf8=</span><span class=\"si\">%E</span><span class=\"s1\">2%9C%93&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">&#39;/♥♥/?utf8=✓&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">uri_to_iri</span><span class=\"p\">(</span><span class=\"s1\">&#39;%A9helloworld&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">&#39;%A9helloworld&#39;</span>\n</code></pre></div>\n<p>In the first example, the UTF-8 characters and reserved characters are\nunquoted. In the second, the percent-encoding remains unchanged because it\nlies outside the valid UTF-8 range.</p>\n<p>Both <code class=\"docutils literal notranslate\"><span class=\"pre\">iri_to_uri()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">uri_to_iri()</span></code> functions are idempotent, which means the\nfollowing is always true:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">iri_to_uri</span><span class=\"p\">(</span><span class=\"n\">iri_to_uri</span><span class=\"p\">(</span><span class=\"n\">some_string</span><span class=\"p\">))</span> <span class=\"o\">==</span> <span class=\"n\">iri_to_uri</span><span class=\"p\">(</span><span class=\"n\">some_string</span><span class=\"p\">)</span>\n<span class=\"n\">uri_to_iri</span><span class=\"p\">(</span><span class=\"n\">uri_to_iri</span><span class=\"p\">(</span><span class=\"n\">some_string</span><span class=\"p\">))</span> <span class=\"o\">==</span> <span class=\"n\">uri_to_iri</span><span class=\"p\">(</span><span class=\"n\">some_string</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>So you can safely call it multiple times on the same URI/IRI without risking\ndouble-quoting problems.</p>\n</section>\n</section>\n</section>\n<section id=\"models\">\n<h2>Models<a class=\"heading-anchor\" href=\"#models\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Because all strings are returned from the database as Unicode strings, model\nfields that are character based (CharField, TextField, URLField, etc.) will\ncontain Unicode values when Django retrieves data from the database. This\nis <em>always</em> the case, even if the data could fit into an ASCII bytestring.</p>\n<p>You can pass in bytestrings when creating a model or populating a field, and\nDjango will convert it to Unicode when it needs to.</p>\n<section id=\"choosing-between-str-and-unicode\">\n<h3>Choosing between <code class=\"docutils literal notranslate\"><span class=\"pre\">__str__()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">__unicode__()</span></code><a class=\"heading-anchor\" href=\"#choosing-between-str-and-unicode\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h3>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>If you are on Python 3, you can skip this section because you’ll always\ncreate <code class=\"docutils literal notranslate\"><span class=\"pre\">__str__()</span></code> rather than <code class=\"docutils literal notranslate\"><span class=\"pre\">__unicode__()</span></code>. If you’d like\ncompatibility with Python 2, you can decorate your model class with\n<a class=\"reference internal\" href=\"/pt-br/1.11/ref/utils/#django.utils.encoding.python_2_unicode_compatible\" title=\"django.utils.encoding.python_2_unicode_compatible\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">python_2_unicode_compatible()</span></code></a>.</p>\n</aside>\n<p>One consequence of using Unicode by default is that you have to take some care\nwhen printing data from the model.</p>\n<p>In particular, rather than giving your model a <code class=\"docutils literal notranslate\"><span class=\"pre\">__str__()</span></code> method, we\nrecommended you implement a <code class=\"docutils literal notranslate\"><span class=\"pre\">__unicode__()</span></code> method. In the <code class=\"docutils literal notranslate\"><span class=\"pre\">__unicode__()</span></code>\nmethod, you can quite safely return the values of all your fields without\nhaving to worry about whether they fit into a bytestring or not. (The way\nPython works, the result of <code class=\"docutils literal notranslate\"><span class=\"pre\">__str__()</span></code> is <em>always</em> a bytestring, even if you\naccidentally try to return a Unicode object).</p>\n<p>You can still create a <code class=\"docutils literal notranslate\"><span class=\"pre\">__str__()</span></code> method on your models if you want, of\ncourse, but you shouldn’t need to do this unless you have a good reason.\nDjango’s <code class=\"docutils literal notranslate\"><span class=\"pre\">Model</span></code> base class automatically provides a <code class=\"docutils literal notranslate\"><span class=\"pre\">__str__()</span></code>\nimplementation that calls <code class=\"docutils literal notranslate\"><span class=\"pre\">__unicode__()</span></code> and encodes the result into UTF-8.\nThis means you’ll normally only need to implement a <code class=\"docutils literal notranslate\"><span class=\"pre\">__unicode__()</span></code> method\nand let Django handle the coercion to a bytestring when required.</p>\n</section>\n<section id=\"taking-care-in-get-absolute-url\">\n<h3>Taking care in <code class=\"docutils literal notranslate\"><span class=\"pre\">get_absolute_url()</span></code><a class=\"heading-anchor\" href=\"#taking-care-in-get-absolute-url\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>URLs can only contain ASCII characters. If you’re constructing a URL from\npieces of data that might be non-ASCII, be careful to encode the results in a\nway that is suitable for a URL. The <a class=\"reference internal\" href=\"/pt-br/1.11/ref/urlresolvers/#django.urls.reverse\" title=\"django.urls.reverse\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">reverse()</span></code></a> function\nhandles this for you automatically.</p>\n<p>If you’re constructing a URL manually (i.e., <em>not</em> using the <code class=\"docutils literal notranslate\"><span class=\"pre\">reverse()</span></code>\nfunction), you’ll need to take care of the encoding yourself. In this case,\nuse the <code class=\"docutils literal notranslate\"><span class=\"pre\">iri_to_uri()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">urlquote()</span></code> functions that were documented\n<a class=\"reference internal\" href=\"#id1\">above</a>. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils.encoding</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">iri_to_uri</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">urlquote</span>\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">get_absolute_url</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    <span class=\"n\">url</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;/person/</span><span class=\"si\">%s</span><span class=\"s1\">/?x=0&amp;y=0&#39;</span> <span class=\"o\">%</span> <span class=\"n\">urlquote</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">location</span><span class=\"p\">)</span>\n    <span class=\"k\">return</span> <span class=\"n\">iri_to_uri</span><span class=\"p\">(</span><span class=\"n\">url</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>This function returns a correctly encoded URL even if <code class=\"docutils literal notranslate\"><span class=\"pre\">self.location</span></code> is\nsomething like “Jack visited Paris &amp; Orléans”. (In fact, the <code class=\"docutils literal notranslate\"><span class=\"pre\">iri_to_uri()</span></code>\ncall isn’t strictly necessary in the above example, because all the\nnon-ASCII characters would have been removed in quoting in the first line.)</p>\n</section>\n</section>\n<section id=\"the-database-api\">\n<h2>The database API<a class=\"heading-anchor\" href=\"#the-database-api\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>You can pass either Unicode strings or UTF-8 bytestrings as arguments to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">filter()</span></code> methods and the like in the database API. The following two\nquerysets are identical:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">__future__</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">unicode_literals</span>\n\n<span class=\"n\">qs</span> <span class=\"o\">=</span> <span class=\"n\">People</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">name__contains</span><span class=\"o\">=</span><span class=\"s1\">&#39;Å&#39;</span><span class=\"p\">)</span>\n<span class=\"n\">qs</span> <span class=\"o\">=</span> <span class=\"n\">People</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">name__contains</span><span class=\"o\">=</span><span class=\"sa\">b</span><span class=\"s1\">&#39;</span><span class=\"se\">\\xc3\\x85</span><span class=\"s1\">&#39;</span><span class=\"p\">)</span> <span class=\"c1\"># UTF-8 encoding of Å</span>\n</code></pre></div>\n</section>\n<section id=\"templates\">\n<h2>Templates<a class=\"heading-anchor\" href=\"#templates\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>You can use either Unicode or bytestrings when creating templates manually:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">__future__</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">unicode_literals</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Template</span>\n<span class=\"n\">t1</span> <span class=\"o\">=</span> <span class=\"n\">Template</span><span class=\"p\">(</span><span class=\"sa\">b</span><span class=\"s1\">&#39;This is a bytestring template.&#39;</span><span class=\"p\">)</span>\n<span class=\"n\">t2</span> <span class=\"o\">=</span> <span class=\"n\">Template</span><span class=\"p\">(</span><span class=\"s1\">&#39;This is a Unicode template.&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>But the common case is to read templates from the filesystem, and this creates\na slight complication: not all filesystems store their data encoded as UTF-8.\nIf your template files are not stored with a UTF-8 encoding, set the <a class=\"reference internal\" href=\"/pt-br/1.11/ref/settings/#std-setting-FILE_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">FILE_CHARSET</span></code></a>\nsetting to the encoding of the files on disk. When Django reads in a template\nfile, it will convert the data from this encoding to Unicode. (<a class=\"reference internal\" href=\"/pt-br/1.11/ref/settings/#std-setting-FILE_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">FILE_CHARSET</span></code></a>\nis set to <code class=\"docutils literal notranslate\"><span class=\"pre\">'utf-8'</span></code> by default.)</p>\n<p>The <a class=\"reference internal\" href=\"/pt-br/1.11/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_CHARSET</span></code></a> setting controls the encoding of rendered templates.\nThis is set to UTF-8 by default.</p>\n<section id=\"template-tags-and-filters\">\n<h3>Template tags and filters<a class=\"heading-anchor\" href=\"#template-tags-and-filters\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>A couple of tips to remember when writing your own template tags and filters:</p>\n<ul class=\"simple\">\n<li><p>Always return Unicode strings from a template tag’s <code class=\"docutils literal notranslate\"><span class=\"pre\">render()</span></code> method\nand from template filters.</p></li>\n<li><p>Use <code class=\"docutils literal notranslate\"><span class=\"pre\">force_text()</span></code> in preference to <code class=\"docutils literal notranslate\"><span class=\"pre\">smart_text()</span></code> in these\nplaces. Tag rendering and filter calls occur as the template is being\nrendered, so there is no advantage to postponing the conversion of lazy\ntranslation objects into strings. It’s easier to work solely with Unicode\nstrings at that point.</p></li>\n</ul>\n</section>\n</section>\n<section id=\"files\">\n<span id=\"unicode-files\"></span><h2>Files<a class=\"heading-anchor\" href=\"#files\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If you intend to allow users to upload files, you must ensure that the\nenvironment used to run Django is configured to work with non-ASCII file names.\nIf your environment isn’t configured correctly, you’ll encounter\n<code class=\"docutils literal notranslate\"><span class=\"pre\">UnicodeEncodeError</span></code> exceptions when saving files with file names that\ncontain non-ASCII characters.</p>\n<p>Filesystem support for UTF-8 file names varies and might depend on the\nenvironment. Check your current configuration in an interactive Python shell by\nrunning:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">sys</span>\n<span class=\"n\">sys</span><span class=\"o\">.</span><span class=\"n\">getfilesystemencoding</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>This should output “UTF-8”.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">LANG</span></code> environment variable is responsible for setting the expected\nencoding on Unix platforms. Consult the documentation for your operating system\nand application server for the appropriate syntax and location to set this\nvariable.</p>\n<p>In your development environment, you might need to add a setting to your\n<code class=\"docutils literal notranslate\"><span class=\"pre\">~.bashrc</span></code> analogous to::</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">export</span> <span class=\"n\">LANG</span><span class=\"o\">=</span><span class=\"s2\">&quot;en_US.UTF-8&quot;</span>\n</code></pre></div>\n</section>\n<section id=\"email\">\n<h2>Email<a class=\"heading-anchor\" href=\"#email\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django’s email framework (in <code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.mail</span></code>) supports Unicode\ntransparently. You can use Unicode data in the message bodies and any headers.\nHowever, you’re still obligated to respect the requirements of the email\nspecifications, so, for example, email addresses should use only ASCII\ncharacters.</p>\n<p>The following code example demonstrates that everything except email addresses\ncan be non-ASCII:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">__future__</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">unicode_literals</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.mail</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">EmailMessage</span>\n\n<span class=\"n\">subject</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;My visit to Sør-Trøndelag&#39;</span>\n<span class=\"n\">sender</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;Arnbjörg Ráðormsdóttir &lt;arnbjorg@example.com&gt;&#39;</span>\n<span class=\"n\">recipients</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;Fred &lt;fred@example.com&#39;</span><span class=\"p\">]</span>\n<span class=\"n\">body</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;...&#39;</span>\n<span class=\"n\">msg</span> <span class=\"o\">=</span> <span class=\"n\">EmailMessage</span><span class=\"p\">(</span><span class=\"n\">subject</span><span class=\"p\">,</span> <span class=\"n\">body</span><span class=\"p\">,</span> <span class=\"n\">sender</span><span class=\"p\">,</span> <span class=\"n\">recipients</span><span class=\"p\">)</span>\n<span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">attach</span><span class=\"p\">(</span><span class=\"s2\">&quot;Une pièce jointe.pdf&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;%PDF-1.4.%...&quot;</span><span class=\"p\">,</span> <span class=\"n\">mimetype</span><span class=\"o\">=</span><span class=\"s2\">&quot;application/pdf&quot;</span><span class=\"p\">)</span>\n<span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">send</span><span class=\"p\">()</span>\n</code></pre></div>\n</section>\n<section id=\"form-submission\">\n<h2>Form submission<a class=\"heading-anchor\" href=\"#form-submission\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>HTML form submission is a tricky area. There’s no guarantee that the\nsubmission will include encoding information, which means the framework might\nhave to guess at the encoding of submitted data.</p>\n<p>Django adopts a “lazy” approach to decoding form data. The data in an\n<code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> object is only decoded when you access it. In fact, most of\nthe data is not decoded at all. Only the <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest.GET</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest.POST</span></code> data structures have any decoding applied to them. Those\ntwo fields will return their members as Unicode data. All other attributes and\nmethods of <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> return data exactly as it was submitted by the\nclient.</p>\n<p>By default, the <a class=\"reference internal\" href=\"/pt-br/1.11/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_CHARSET</span></code></a> setting is used as the assumed encoding\nfor form data. If you need to change this for a particular form, you can set\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">encoding</span></code> attribute on an <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> instance. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">some_view</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"c1\"># We know that the data must be encoded as KOI8-R (for some reason).</span>\n    <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">encoding</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;koi8-r&#39;</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>You can even change the encoding after having accessed <code class=\"docutils literal notranslate\"><span class=\"pre\">request.GET</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">request.POST</span></code>, and all subsequent accesses will use the new encoding.</p>\n<p>Most developers won’t need to worry about changing form encoding, but this is\na useful feature for applications that talk to legacy systems whose encoding\nyou cannot control.</p>\n<p>Django does not decode the data of file uploads, because that data is normally\ntreated as collections of bytes, rather than strings. Any automatic decoding\nthere would alter the meaning of the stream of bytes.</p>\n</section>","rootId":"unicode-data","toc":[{"title":"Creating the database","anchor":"creating-the-database","children":[]},{"title":"General string handling","anchor":"general-string-handling","children":[{"title":"Translated strings","anchor":"translated-strings","children":[]},{"title":"Useful utility functions","anchor":"useful-utility-functions","children":[{"title":"Conversion functions","anchor":"conversion-functions","children":[]},{"title":"URI and IRI handling","anchor":"uri-and-iri-handling","children":[]}]}]},{"title":"Models","anchor":"models","children":[{"title":"Choosing between __str__() and __unicode__()","anchor":"choosing-between-str-and-unicode","children":[]},{"title":"Taking care in get_absolute_url()","anchor":"taking-care-in-get-absolute-url","children":[]}]},{"title":"The database API","anchor":"the-database-api","children":[]},{"title":"Templates","anchor":"templates","children":[{"title":"Template tags and filters","anchor":"template-tags-and-filters","children":[]}]},{"title":"Files","anchor":"files","children":[]},{"title":"Email","anchor":"email","children":[]},{"title":"Form submission","anchor":"form-submission","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"Referência da API","url":"/pt-br/1.11/ref/"}],"prev":{"docname":"ref/template-response","title":"TemplateResponse and SimpleTemplateResponse","url":"/pt-br/1.11/ref/template-response/"},"next":{"docname":"ref/urlresolvers","title":"django.urls utility functions","url":"/pt-br/1.11/ref/urlresolvers/"},"formats":{"html":"/pt-br/1.11/ref/unicode/","markdown":"/pt-br/1.11/ref/unicode.md","json":"/pt-br/1.11/ref/unicode.json"},"source":"https://github.com/django/django/blob/stable/1.11.x/docs/ref/unicode.txt","official":"https://docs.djangoproject.com/pt-br/1.11/ref/unicode/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","fr","ja","id","pt-br","ko","es","el","pl"]}