{"title":"Unicode data","version":"4.2","locale":"pt-br","docname":"ref/unicode","url":"/pt-br/4.2/ref/unicode/","canonical":"https://djangodocs.dev/pt-br/4.2/ref/unicode/","summary":"Django supports Unicode data everywhere. This document tells you what you need to know if you’re writing applications that use data or templates that are encoded in…","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 supports Unicode data everywhere.</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/multibyte.html#id-1.6.11.5.6\">PostgreSQL manual</a> for details on creating\ndatabases with the correct encoding.</p></li>\n<li><p>Oracle users, refer to the <a class=\"reference external\" href=\"https://docs.oracle.com/en/database/oracle/oracle-database/21/nlspg/index.html\">Oracle manual</a> for details on how to set\n(<a class=\"reference external\" href=\"https://docs.oracle.com/en/database/oracle/oracle-database/21/nlspg/choosing-character-set.html\">section 2</a>) or alter (<a class=\"reference external\" href=\"https://docs.oracle.com/en/database/oracle/oracle-database/21/nlspg/character-set-migration.html\">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 strings into\nthe appropriate encoding for talking to the database. They also automatically\nconvert strings retrieved from the database into strings. You don’t even need\nto tell Django what encoding your database uses: that is handled 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 normal strings or bytestrings (starting with a ‘b’).</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/4.2/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/4.2/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/4.2/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\nstrings before doing anything else. So, as a general rule, if you pass\nin a bytestring, be prepared to receive a 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 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\">str()</span></code> with the lazy translation as the argument will generate a\nstring 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/4.2/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 string 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 strings and bytestrings.</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">smart_str(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 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\">str()</span></code> function for its error\nhandling.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">force_str(s,</span> <span class=\"pre\">encoding='utf-8',</span> <span class=\"pre\">strings_only=False,</span> <span class=\"pre\">errors='strict')</span></code> is\nidentical to <code class=\"docutils literal notranslate\"><span class=\"pre\">smart_str()</span></code> in almost all cases. The difference is when the\nfirst argument is a <a class=\"reference internal\" href=\"/pt-br/4.2/topics/i18n/translation/#lazy-translations\"><span class=\"std std-ref\">lazy translation</span></a> instance.\nWhile <code class=\"docutils literal notranslate\"><span class=\"pre\">smart_str()</span></code> preserves lazy translations, <code class=\"docutils literal notranslate\"><span class=\"pre\">force_str()</span></code> forces\nthose objects to a string (causing the translation to occur). Normally,\nyou’ll want to use <code class=\"docutils literal notranslate\"><span class=\"pre\">smart_str()</span></code>. However, <code class=\"docutils literal notranslate\"><span class=\"pre\">force_str()</span></code> is useful in\ntemplate tags and filters that absolutely <em>must</em> have a string to work with,\nnot 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_str()</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_str()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">force_str()</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_str()</span></code>. Call it as early as\npossible on any input data that might be either a string or a bytestring, and\nfrom then on, you can treat the result as always being a string.</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 IRI). 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 <span class=\"target\" id=\"index-0\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc3987.html\"><strong>IRI</strong></a> – very loosely speaking, a <span class=\"target\" id=\"index-1\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc3986.html\"><strong>URI</strong></a>\nthat can contain Unicode characters. Use these functions for quoting and\nconverting an IRI to a URI:</p>\n<ul class=\"simple\">\n<li><p>The <a class=\"reference internal\" href=\"/pt-br/4.2/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> function, which implements the\nconversion from IRI to URI as required by <span class=\"target\" id=\"index-2\"></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 <a class=\"reference external\" href=\"https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote\" title=\"(em Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">urllib.parse.quote()</span></code></a> and <a class=\"reference external\" href=\"https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote_plus\" title=\"(em Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">urllib.parse.quote_plus()</span></code></a>\nfunctions from Python’s standard library.</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\">quote()</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=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">urllib.parse</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">quote</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><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=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">quote</span><span class=\"p\">(</span><span class=\"s2\">&quot;Paris &amp; Orléans&quot;</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=\"s2\">&quot;/favorites/François/</span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span> <span class=\"o\">%</span> <span class=\"n\">quote</span><span class=\"p\">(</span><span class=\"s2\">&quot;Paris &amp; Orléans&quot;</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\">quote()</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/4.2/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-3\"></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>.</p>\n<p>An example to demonstrate:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><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\">uri_to_iri</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">uri_to_iri</span><span class=\"p\">(</span><span class=\"s2\">&quot;/</span><span class=\"si\">%E</span><span class=\"s2\">2</span><span class=\"si\">%99%</span><span class=\"s2\">A5</span><span class=\"si\">%E</span><span class=\"s2\">2</span><span class=\"si\">%99%</span><span class=\"s2\">A5/?utf8=</span><span class=\"si\">%E</span><span class=\"s2\">2%9C%93&quot;</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=\"s2\">&quot;%A9hello</span><span class=\"si\">%3F</span><span class=\"s2\">world&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">&#39;%A9hello%3Fworld&#39;</span>\n</code></pre></div>\n<p>In the first example, the UTF-8 characters are unquoted. In the second, the\npercent-encodings remain unchanged because they lie outside the valid UTF-8\nrange or represent a reserved character.</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 <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code> objects, 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 strings when it needs to.</p>\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/4.2/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\">quote()</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\">urllib.parse</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">quote</span>\n<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\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=\"s2\">&quot;/person/</span><span class=\"si\">%s</span><span class=\"s2\">/?x=0&amp;y=0&quot;</span> <span class=\"o\">%</span> <span class=\"n\">quote</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=\"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>Use strings 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\">django.template</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Template</span>\n\n<span class=\"n\">t2</span> <span class=\"o\">=</span> <span class=\"n\">Template</span><span class=\"p\">(</span><span class=\"s2\">&quot;This is a string template.&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>But the common case is to read templates from the filesystem. If your template\nfiles are not stored with a UTF-8 encoding, adjust the <a class=\"reference internal\" href=\"/pt-br/4.2/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a>\nsetting. The built-in <a class=\"reference internal\" href=\"/pt-br/4.2/topics/templates/#module-django.template.backends.django\" title=\"django.template.backends.django\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django</span></code></a> backend\nprovides the <code class=\"docutils literal notranslate\"><span class=\"pre\">'file_charset'</span></code> option to change the encoding used to read\nfiles from disk.</p>\n<p>The <a class=\"reference internal\" href=\"/pt-br/4.2/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 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_str()</span></code> in preference to <code class=\"docutils literal notranslate\"><span class=\"pre\">smart_str()</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\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 or content\nthat contains 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\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. See the <a class=\"reference internal\" href=\"/pt-br/4.2/howto/deployment/wsgi/modwsgi/\"><span class=\"doc\">Como usar o Django com Apache e mod_wsgi</span></a> for examples.</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=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</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=\"Shell code\"><code><span class=\"nb\">export</span><span class=\"w\"> </span><span class=\"nv\">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=\"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/4.2/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=\"s2\">&quot;koi8-r&quot;</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":"Taking care in get_absolute_url()","anchor":"taking-care-in-get-absolute-url","children":[]}]},{"title":"Templates","anchor":"templates","children":[{"title":"Template tags and filters","anchor":"template-tags-and-filters","children":[]}]},{"title":"Files","anchor":"files","children":[]},{"title":"Form submission","anchor":"form-submission","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"Referência da API","url":"/pt-br/4.2/ref/"}],"prev":{"docname":"ref/template-response","title":"TemplateResponse and SimpleTemplateResponse","url":"/pt-br/4.2/ref/template-response/"},"next":{"docname":"ref/urlresolvers","title":"django.urls utility functions","url":"/pt-br/4.2/ref/urlresolvers/"},"formats":{"html":"/pt-br/4.2/ref/unicode/","markdown":"/pt-br/4.2/ref/unicode.md","json":"/pt-br/4.2/ref/unicode.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/ref/unicode.txt","official":"https://docs.djangoproject.com/pt-br/4.2/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","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}