{"title":"Format localization","version":"6.1","locale":"it","docname":"topics/i18n/formatting","url":"/it/6.1/topics/i18n/formatting/","canonical":"https://djangodocs.dev/it/6.1/topics/i18n/formatting/","summary":"Overview Link to this heading # Django’s formatting system is capable of displaying dates, times and numbers in templates using the format specified for the current…","html":"<h1>Format localization<a class=\"heading-anchor\" href=\"#format-localization\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<section id=\"overview\">\n<h2>Overview<a class=\"heading-anchor\" href=\"#overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django’s formatting system is capable of displaying dates, times and numbers in\ntemplates using the format specified for the current\n<a class=\"reference internal\" href=\"/it/6.1/topics/i18n/#term-locale-name\"><span class=\"xref std std-term\">locale</span></a>. It also handles localized input in forms.</p>\n<p>Two users accessing the same content may see dates, times and numbers formatted\nin different ways, depending on the formats for their current locale.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>To enable number formatting with thousand separators, it is necessary to\nset <a class=\"reference internal\" href=\"/it/6.1/ref/settings/#std-setting-USE_THOUSAND_SEPARATOR\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_THOUSAND_SEPARATOR</span> <span class=\"pre\">=</span> <span class=\"pre\">True</span></code></a> in\nyour settings file. Alternatively, you could use <a class=\"reference internal\" href=\"/it/6.1/ref/contrib/humanize/#std-templatefilter-intcomma\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">intcomma</span></code></a> to\nformat numbers in your template.</p>\n</aside>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>There is a related <a class=\"reference internal\" href=\"/it/6.1/ref/settings/#std-setting-USE_I18N\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_I18N</span></code></a> setting that controls if Django\nshould activate translation. See <a class=\"reference internal\" href=\"/it/6.1/topics/i18n/translation/\"><span class=\"doc\">Translation</span></a> for more\ndetails.</p>\n</aside>\n</section>\n<section id=\"locale-aware-input-in-forms\">\n<h2>Locale aware input in forms<a class=\"heading-anchor\" href=\"#locale-aware-input-in-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When formatting is enabled, Django can use localized formats when parsing\ndates, times and numbers in forms. That means it tries different formats for\ndifferent locales when guessing the format used by the user when inputting data\non forms.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>Django uses different formats for displaying data to those it uses for\nparsing data. Most notably, the formats for parsing dates can’t use the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">%a</span></code> (abbreviated weekday name), <code class=\"docutils literal notranslate\"><span class=\"pre\">%A</span></code> (full weekday name),\n<code class=\"docutils literal notranslate\"><span class=\"pre\">%b</span></code> (abbreviated month name), <code class=\"docutils literal notranslate\"><span class=\"pre\">%B</span></code> (full month name),\nor <code class=\"docutils literal notranslate\"><span class=\"pre\">%p</span></code> (AM/PM).</p>\n</aside>\n<p>To enable a form field to localize input and output data use its <code class=\"docutils literal notranslate\"><span class=\"pre\">localize</span></code>\nargument:</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\">class</span><span class=\"w\"> </span><span class=\"nc\">CashRegisterForm</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">Form</span><span class=\"p\">):</span>\n    <span class=\"n\">product</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">()</span>\n    <span class=\"n\">revenue</span> <span class=\"o\">=</span> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">DecimalField</span><span class=\"p\">(</span><span class=\"n\">max_digits</span><span class=\"o\">=</span><span class=\"mi\">4</span><span class=\"p\">,</span> <span class=\"n\">decimal_places</span><span class=\"o\">=</span><span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"n\">localize</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n</code></pre></div>\n<section id=\"locale-specific-helpers\">\n<h3>Locale specific helpers<a class=\"heading-anchor\" href=\"#locale-specific-helpers\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>For country specific form fields, validation, and data formats that are not\nincluded in Django itself, consider using the <a class=\"extlink-pypi reference external\" href=\"https://pypi.org/project/django-localflavor/\">django-localflavor</a>\npackage, which provides localized form fields and utilities for many countries\nand regions (<a class=\"reference external\" href=\"https://django-localflavor.readthedocs.io/\">documentation</a>).</p>\n</section>\n</section>\n<section id=\"controlling-localization-in-templates\">\n<span id=\"topic-l10n-templates\"></span><h2>Controlling localization in templates<a class=\"heading-anchor\" href=\"#controlling-localization-in-templates\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django tries to use a locale specific format whenever it outputs a value in a\ntemplate.</p>\n<p>However, it may not always be appropriate to use localized values –\nfor example, if you’re outputting JavaScript or XML that is designed\nto be machine-readable, you will always want unlocalized values. You\nmay also want to use localization in selected templates, rather than\nusing localization everywhere.</p>\n<p>To allow for fine control over the use of localization, Django\nprovides the <code class=\"docutils literal notranslate\"><span class=\"pre\">l10n</span></code> template library that contains the following\ntags and filters.</p>\n<section id=\"template-tags\">\n<h3>Template tags<a class=\"heading-anchor\" href=\"#template-tags\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"localize\">\n<span id=\"std-templatetag-localize\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">localize</span></code><a class=\"heading-anchor\" href=\"#localize\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Enables or disables localization of template variables in the\ncontained block.</p>\n<p>To activate or deactivate localization for a template block, use:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">l10n</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">localize</span> <span class=\"nv\">on</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">value</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endlocalize</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">localize</span> <span class=\"nv\">off</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">value</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endlocalize</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>When localization is disabled, the <a class=\"reference internal\" href=\"/it/6.1/ref/settings/#settings-l10n\"><span class=\"std std-ref\">localization settings</span></a>\nformats are applied.</p>\n<p>See <a class=\"reference internal\" href=\"#std-templatefilter-localize\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">localize</span></code></a> and <a class=\"reference internal\" href=\"#std-templatefilter-unlocalize\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">unlocalize</span></code></a> for template filters that\nwill do the same job on a per-variable basis.</p>\n</section>\n</section>\n<section id=\"template-filters\">\n<h3>Template filters<a class=\"heading-anchor\" href=\"#template-filters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"std-templatefilter-localize\">\n<span id=\"id1\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">localize</span></code><a class=\"heading-anchor\" href=\"#std-templatefilter-localize\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Forces localization of a single value.</p>\n<p>For example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">l10n</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">localize</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>To disable localization on a single value, use <a class=\"reference internal\" href=\"#std-templatefilter-unlocalize\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">unlocalize</span></code></a>. To\ncontrol localization over a large section of a template, use the\n<a class=\"reference internal\" href=\"#std-templatetag-localize\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">localize</span></code></a> template tag.</p>\n</section>\n<section id=\"unlocalize\">\n<span id=\"std-templatefilter-unlocalize\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">unlocalize</span></code><a class=\"heading-anchor\" href=\"#unlocalize\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Forces a single value to be printed without localization.</p>\n<p>For example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">l10n</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">unlocalize</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>To force localization of a single value, use <a class=\"reference internal\" href=\"#std-templatefilter-localize\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">localize</span></code></a>. To\ncontrol localization over a large section of a template, use the\n<a class=\"reference internal\" href=\"#std-templatetag-localize\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">localize</span></code></a> template tag.</p>\n<p>Returns a string representation for numbers  (<code class=\"docutils literal notranslate\"><span class=\"pre\">int</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">float</span></code>, or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Decimal</span></code>) with the <a class=\"reference internal\" href=\"/it/6.1/ref/settings/#settings-l10n\"><span class=\"std std-ref\">localization settings</span></a> formats\napplied.</p>\n</section>\n</section>\n</section>\n<section id=\"creating-custom-format-files\">\n<span id=\"custom-format-files\"></span><h2>Creating custom format files<a class=\"heading-anchor\" href=\"#creating-custom-format-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django provides format definitions for many locales, but sometimes you might\nwant to create your own, because a format file doesn’t exist for your locale,\nor because you want to overwrite some of the values.</p>\n<p>To use custom formats, specify the path where you’ll place format files\nfirst. To do that, set your <a class=\"reference internal\" href=\"/it/6.1/ref/settings/#std-setting-FORMAT_MODULE_PATH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">FORMAT_MODULE_PATH</span></code></a> setting to the\npackage where format files will exist, for instance:</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\">FORMAT_MODULE_PATH</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s2\">&quot;mysite.formats&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;some_app.formats&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Files are not placed directly in this directory, but in a directory named as\nthe locale, and must be named <code class=\"docutils literal notranslate\"><span class=\"pre\">formats.py</span></code>. Be careful not to put sensitive\ninformation in these files as values inside can be exposed if you pass the\nstring to <code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.formats.get_format()</span></code> (used by the <a class=\"reference internal\" href=\"/it/6.1/ref/templates/builtins/#std-templatefilter-date\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">date</span></code></a>\ntemplate filter).</p>\n<p>To customize the English formats, a structure like this would be needed:</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>mysite/\n    formats/\n        __init__.py\n        en/\n            __init__.py\n            formats.py\n</code></pre></div>\n<p>where <code class=\"file docutils literal notranslate\"><span class=\"pre\">formats.py</span></code> contains custom format definitions. 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=\"n\">THOUSAND_SEPARATOR</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;</span><span class=\"se\">\\xa0</span><span class=\"s2\">&quot;</span>\n</code></pre></div>\n<p>to use a non-breaking space (Unicode <code class=\"docutils literal notranslate\"><span class=\"pre\">00A0</span></code>) as a thousand separator,\ninstead of the default for English, a comma.</p>\n</section>\n<section id=\"limitations-of-the-provided-locale-formats\">\n<h2>Limitations of the provided locale formats<a class=\"heading-anchor\" href=\"#limitations-of-the-provided-locale-formats\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Some locales use context-sensitive formats for numbers, which Django’s\nlocalization system cannot handle automatically.</p>\n<section id=\"switzerland-german-french\">\n<h3>Switzerland (German, French)<a class=\"heading-anchor\" href=\"#switzerland-german-french\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The Swiss number formatting traditionally varies depending on context. For\nexample, monetary values may use a dot as decimal separator (<code class=\"docutils literal notranslate\"><span class=\"pre\">Fr.</span> <span class=\"pre\">23.50</span></code>),\nwhile measurements often use a comma (<code class=\"docutils literal notranslate\"><span class=\"pre\">22,5</span> <span class=\"pre\">m</span></code>). Django’s localization system\ndoes not support such context-specific variations automatically.</p>\n<p>The locale format provided by Django uses the generic separators, a comma for\ndecimal and a space for thousand separators.</p>\n</section>\n</section>","rootId":"format-localization","toc":[{"title":"Overview","anchor":"overview","children":[]},{"title":"Locale aware input in forms","anchor":"locale-aware-input-in-forms","children":[{"title":"Locale specific helpers","anchor":"locale-specific-helpers","children":[]}]},{"title":"Controlling localization in templates","anchor":"controlling-localization-in-templates","children":[{"title":"Template tags","anchor":"template-tags","children":[{"title":"localize","anchor":"localize","children":[]}]},{"title":"Template filters","anchor":"template-filters","children":[{"title":"localize","anchor":"std-templatefilter-localize","children":[]},{"title":"unlocalize","anchor":"unlocalize","children":[]}]}]},{"title":"Creating custom format files","anchor":"creating-custom-format-files","children":[]},{"title":"Limitations of the provided locale formats","anchor":"limitations-of-the-provided-locale-formats","children":[{"title":"Switzerland (German, French)","anchor":"switzerland-german-french","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/it/6.1/topics/"},{"docname":"topics/i18n/index","title":"Internationalization and localization","url":"/it/6.1/topics/i18n/"}],"prev":{"docname":"topics/i18n/translation","title":"Translation","url":"/it/6.1/topics/i18n/translation/"},"next":{"docname":"topics/i18n/timezones","title":"Time zones","url":"/it/6.1/topics/i18n/timezones/"},"formats":{"html":"/it/6.1/topics/i18n/formatting/","markdown":"/it/6.1/topics/i18n/formatting.md","json":"/it/6.1/topics/i18n/formatting.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/topics/i18n/formatting.txt","official":"https://docs.djangoproject.com/it/6.1/topics/i18n/formatting/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}