{"title":"Unicode データ","version":"6.0","locale":"ja","docname":"ref/unicode","url":"/ja/6.0/ref/unicode/","canonical":"https://djangodocs.dev/ja/6.0/ref/unicode/","summary":"Django はどこでも Unicode データをサポートしています。この文書は、ASCII以外のエンコーディングでデータやテンプレートを使用するアプリケーションを書く場合に、知…","html":"<h1>Unicode データ<a class=\"heading-anchor\" href=\"#unicode-data\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django はどこでも Unicode データをサポートしています。</p>\n<p>この文書は、ASCII以外のエンコーディングでデータやテンプレートを使用するアプリケーションを書く場合に、知っておくべきことを説明します。</p>\n<section id=\"creating-the-database\">\n<h2>データベースの作成<a class=\"heading-anchor\" href=\"#creating-the-database\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>データベースが任意の文字列データを保存できるように設定されていることを確認してください。通常、これは UTF-8 または UTF-16 のエンコーディングを使用することを意味します。もっと制限のあるエンコーディング -- 例えば、latin1 (iso8859-1) -- を使用する場合、特定の文字をデータベースに保存することができず、情報が失われることになります。</p>\n<ul class=\"simple\">\n<li><p>MySQLユーザーの場合、データベースの文字セットエンコーディングを設定または変更する方法の詳細については、 <a class=\"reference external\" href=\"https://dev.mysql.com/doc/refman/en/charset-database.html\">MySQL manual</a> を参照してください。</p></li>\n<li><p>PostgreSQL を使用している場合は、適切なエンコーディングでデータベースを作成するための詳細については、 <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/multibyte.html#MULTIBYTE-SETTING\">PostgreSQL manual</a> を参照してください。</p></li>\n<li><p>Oracle ユーザーは、データベースの文字セットエンコーディングを設定（ <a class=\"reference external\" href=\"https://docs.oracle.com/en/database/oracle/oracle-database/21/nlspg/choosing-character-set.html\">section 2</a> ）する方法や変更（ <a class=\"reference external\" href=\"https://docs.oracle.com/en/database/oracle/oracle-database/21/nlspg/character-set-migration.html\">section 11</a> ）する方法の詳細については、 <a class=\"reference external\" href=\"https://docs.oracle.com/en/database/oracle/oracle-database/21/nlspg/index.html\">Oracle manual</a> を参照してください。</p></li>\n<li><p>SQLite を使用しているユーザーは、何もする必要はありません。SQLite は常に UTF-8 を内部エンコーディングに使用します。</p></li>\n</ul>\n<p>Djangoのデータベースバックエンドはすべて自動的に、データベースと通信するために適切なエンコーディングへ文字列を変換します。また、データベースから取得した文字列も自動的に文字列に変換します。データベースが使用しているエンコーディングをDjangoに伝える必要はまったくありません。それは透過的に処理されます。</p>\n<p>詳細については、以下の「データベース API」セクションを参照してください。</p>\n</section>\n<section id=\"general-string-handling\">\n<h2>一般的な文字列操作<a class=\"heading-anchor\" href=\"#general-string-handling\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Djangoを使用する際、例えば、データベース検索やテンプレートのレンダリングなど、どこでも、文字列に対して2つのエンコーディング選択肢があります。通常の文字列を使用するか、バイト文字列（'b' で始まる）を使用できます。</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>バイト文字列は、そのエンコーディングに関する情報を何も持っていません。そのため、我々は仮定を立てなければなりません。そして、Djangoはすべてのバイト文字列がUTF-8であると仮定します。</p>\n<p>Django に他の形式でエンコードされた文字列を渡すと、興味深い方法で問題が発生します。通常、いずれかのタイミングで Django は <code class=\"docutils literal notranslate\">UnicodeDecodeError</code> を発生させます。</p>\n</aside>\n<p>コードが ASCII データのみを使用している場合、ASCII は UTF-8 のサブセットなので、通常の文字列を自由に使って渡すことが安全です。</p>\n<p><a class=\"reference internal\" href=\"/ja/6.0/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_CHARSET</code></a> 設定を <code class=\"docutils literal notranslate\">'utf-8'</code> 以外に設定しているからといって、バイト文字列でその他のエンコーディングを使用できると思わないでください！ <a class=\"reference internal\" href=\"/ja/6.0/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_CHARSET</code></a> は、テンプレートのレンダリング結果（および電子メール）として生成された文字列にのみ適用されます。Django は、内部的なバイト文字列については常に UTF-8 エンコーディングを想定します。その理由は、 <a class=\"reference internal\" href=\"/ja/6.0/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_CHARSET</code></a> 設定が実際には（アプリケーション開発者であるあなたの）制御下にないからです。それは、あなたのアプリケーションをインストールして使用している人が制御しています ― そして、その人が異なる設定を選択した場合でも、あなたのコードは引き続き動作しなければなりません。したがって、その設定に依存することはできません。</p>\n<p>Djangoが文字列を扱う場合、大抵の場合、何をするにもまずそれらを文字列に変換します。従って、一般的なルールとして、バイト文字列を渡した場合は、結果として文字列を受け取る準備をしておくべきです。</p>\n<section id=\"translated-strings\">\n<h3>翻訳された文字列<a class=\"heading-anchor\" href=\"#translated-strings\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>文字列やバイト文字列とは別に、Djangoを使用しているときに遭遇する可能性がある文字列型のオブジェクトがもう一つあります。フレームワークの国際化機能は「遅延翻訳（lazy な翻訳）」という概念を導入しています ― 翻訳済みとマークされているが、オブジェクトが文字列として使用されるまで実際の翻訳結果が決定されない文字列です。この機能は、文字列が最初に作成されたときにはコードがインポートされたにもかかわらず、文字列が使用されるまで翻訳ロケールが不明な場合に便利です。</p>\n<p>通常、遅延翻訳について心配する必要はありません。ただし、オブジェクトを調べたときに <code class=\"docutils literal notranslate\">django.utils.functional.__proxy__</code> オブジェクトであると表示される場合は、それが遅延翻訳であることに注意してください。遅延翻訳を引数として <code class=\"docutils literal notranslate\">str()</code> を呼び出すと、現在のロケールの文字列が生成されます。</p>\n<p>詳細については、 <a class=\"reference internal\" href=\"/ja/6.0/topics/i18n/\"><span class=\"doc\">国際化</span></a> ドキュメントを参照してください。</p>\n</section>\n<section id=\"useful-utility-functions\">\n<h3>便利なユーティリティ関数<a class=\"heading-anchor\" href=\"#useful-utility-functions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>いくつかの文字列操作は度々必要とされるため、Django には文字列やバイト文字列の操作を少し簡単にする便利な関数がいくつか含まれています。</p>\n<section id=\"conversion-functions\">\n<h4>変換関数<a class=\"heading-anchor\" href=\"#conversion-functions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><code class=\"docutils literal notranslate\">django.utils.encoding</code> モジュールには、文字列とバイト列との間で相互に変換するために便利ないくつかの関数が含まれています。</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">smart_str(s, encoding='utf-8', strings_only=False, errors='strict')</code> は、その入力を文字列に変換します。 <code class=\"docutils literal notranslate\">encoding</code> パラメータは入力エンコーディングを指定します（例えば、Djangoはこれを内部で使用して、UTF-8 エンコードされていないかもしれないフォーム入力データを処理します）。 <code class=\"docutils literal notranslate\">strings_only</code> パラメータが True に設定されている場合、Python の数値、真偽値、そして <code class=\"docutils literal notranslate\">None</code> は文字列に変換されずに元の型を保持します。 <code class=\"docutils literal notranslate\">errors</code> パラメータは、Python の <code class=\"docutils literal notranslate\">str()</code> 関数がエラーハンドリングに受け入れる値のいずれかを取ります。</p></li>\n<li><p><code class=\"docutils literal notranslate\">force_str(s, encoding='utf-8', strings_only=False, errors='strict')</code> は、ほとんどの場合 <code class=\"docutils literal notranslate\">smart_str()</code> と同じです。違いは、最初の引数が <a class=\"reference internal\" href=\"/ja/6.0/topics/i18n/translation/#lazy-translations\"><span class=\"std std-ref\">遅延翻訳（Lazyな翻訳）</span></a> インスタンスである場合です。<code class=\"docutils literal notranslate\">smart_str()</code> は遅延翻訳を保持しますが、<code class=\"docutils literal notranslate\">force_str()</code> はそれらのオブジェクトを文字列に強制します（翻訳が発生します）。通常は <code class=\"docutils literal notranslate\">smart_str()</code> を使用しますが、文字列に絶対に <em>変換する必要がある</em> テンプレートタグやフィルタには <code class=\"docutils literal notranslate\">force_str()</code> が役立ちます。</p></li>\n<li><p><code class=\"docutils literal notranslate\">smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict')</code> は基本的に <code class=\"docutils literal notranslate\">smart_str()</code> の反対です。第一引数をバイト文字列に強制します。 <code class=\"docutils literal notranslate\">strings_only</code> パラメータは、<code class=\"docutils literal notranslate\">smart_str()</code> および <code class=\"docutils literal notranslate\">force_str()</code> と同じ動作をします。これは Python の組み込み関数 <code class=\"docutils literal notranslate\">str()</code> とは少し異なる意味論ですが、Django の内部で必要とされるいくつかの場所での違いです。</p></li>\n</ul>\n<p>通常、<code class=\"docutils literal notranslate\">force_str()</code> を使用するだけで十分です。文字列またはバイト文字列の可能性があるあらゆる入力データに対して、できるだけ早くそれを呼び出しましょう。そうすることで、その結果を常に文字列として扱うことができます。</p>\n</section>\n<section id=\"uri-and-iri-handling\">\n<span id=\"id1\"></span><h4>URI と IRI の処理<a class=\"heading-anchor\" href=\"#uri-and-iri-handling\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Web フレームワークは URL（IRI の一種）を扱う必要があります。URL の要件の1つは、ASCII 文字のみを使用してエンコードされることです。しかし、国際的な環境では、Unicode 文字を含むことができる <span class=\"target\" id=\"index-5\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc3987.html\"><strong>IRI</strong></a> から URL を構築する必要があるかもしれません。IRI を URI に引用して変換するためには、これらの関数を使用します。</p>\n<ul class=\"simple\">\n<li><p>The <a class=\"reference internal\" href=\"/ja/6.0/ref/utils/#django.utils.encoding.iri_to_uri\" title=\"django.utils.encoding.iri_to_uri\"><code class=\"xref py py-func docutils literal notranslate\">django.utils.encoding.iri_to_uri()</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>Python 標準ライブラリからの <a class=\"reference external\" href=\"https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">urllib.parse.quote()</code></a> と <a class=\"reference external\" href=\"https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote_plus\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">urllib.parse.quote_plus()</code></a> 関数。</p></li>\n</ul>\n<p>これら2つの関数のグループは、わずかに異なる目的があり、それらを区別することが重要です。通常、&quot;&amp;&quot; や &quot;%&quot; のような予約文字が正しくエンコードされるように、IRI や URI パスの個々の部分に <code class=\"docutils literal notranslate\">quote()</code> を使用します。その後、完全な IRI に <code class=\"docutils literal notranslate\">iri_to_uri()</code> を適用し、非 ASCII 文字を正しいエンコード値に変換します。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>技術的に言うと、<code class=\"docutils literal notranslate\">iri_to_uri()</code> が IRI 仕様の完全なアルゴリズムを実装していると言うのは正確ではありません。まだ、アルゴリズムの国際ドメイン名エンコーディング部分を実行していません。</p>\n</aside>\n<p><code class=\"docutils literal notranslate\">iri_to_uri()</code> 関数は、URL で許可されている ASCII 文字を変更しません。したがって、例えば、文字 &quot;%&quot; は <code class=\"docutils literal notranslate\">iri_to_uri()</code> に渡されたときに、さらにエンコードされることはありません。これは、完全な URL をこの関数に渡しても、クエリ文字列やそれに類するものを台無しにすることはないという意味です。</p>\n<p>ここで例を挙げるとより理解しやすくなります:</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=\"nn\">urllib.parse</span> <span class=\"kn\">import</span> quote\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.utils.encoding</span> <span class=\"kn\">import</span> iri_to_uri\n<span class=\"gp\">&gt;&gt;&gt; </span>quote<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>iri_to_uri<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> quote<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>よく見ると、2番目の例で <code class=\"docutils literal notranslate\">quote()</code> によって生成された部分が <code class=\"docutils literal notranslate\">iri_to_uri()</code> に渡された際に二重引用符で囲まれていないことがわかります。これは非常に重要で便利な機能です。つまり、IRIを構築する際に、非ASCII文字が含まれているかどうかを心配せずに、最後になってから結果に対して <code class=\"docutils literal notranslate\">iri_to_uri()</code> を呼び出すことができるということです。</p>\n<p>Similarly, Django provides <a class=\"reference internal\" href=\"/ja/6.0/ref/utils/#django.utils.encoding.uri_to_iri\" title=\"django.utils.encoding.uri_to_iri\"><code class=\"xref py py-func docutils literal notranslate\">django.utils.encoding.uri_to_iri()</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>デモンストレーションの例:</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=\"nn\">django.utils.encoding</span> <span class=\"kn\">import</span> uri_to_iri\n<span class=\"gp\">&gt;&gt;&gt; </span>uri_to_iri<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>uri_to_iri<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>最初の例では、UTF-8 の文字はクォートされていません。2番目の例では、パーセントエンコーディングは、有効な UTF-8 の範囲外にあるか、予約された文字を表しているため、変更されません。</p>\n<p><code class=\"docutils literal notranslate\">iri_to_uri()</code> と <code class=\"docutils literal notranslate\">uri_to_iri()</code> 関数は冪等であり、つまり以下が常に真であることを意味します。</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>iri_to_uri<span class=\"p\">(</span>iri_to_uri<span class=\"p\">(</span>some_string<span class=\"p\">))</span> <span class=\"o\">==</span> iri_to_uri<span class=\"p\">(</span>some_string<span class=\"p\">)</span>\nuri_to_iri<span class=\"p\">(</span>uri_to_iri<span class=\"p\">(</span>some_string<span class=\"p\">))</span> <span class=\"o\">==</span> uri_to_iri<span class=\"p\">(</span>some_string<span class=\"p\">)</span>\n</code></pre></div>\n<p>同じURI/IRIに対して複数回安全に呼び出すことができます。二重引用符の問題を気にする必要はありません。</p>\n</section>\n</section>\n</section>\n<section id=\"models\">\n<h2>モデル<a class=\"heading-anchor\" href=\"#models\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>データベースから返される全ての文字列が <code class=\"docutils literal notranslate\">str</code> オブジェクトとして返されるため、文字ベースのモデルフィールド(CharField、TextField、URLField など)には、Django がデータベースからデータを取得する際、Unicode の値が格納されます。これは、データが ASCII バイト文字列に収まる場合でも、 <em>常に</em> 当てはまります。</p>\n<p>モデルを作成する際やフィールドに値を設定する際にバイト文字列を渡すことができ、Django は必要に応じてそれを文字列に変換します。</p>\n<section id=\"taking-care-in-get-absolute-url\">\n<h3><code class=\"docutils literal notranslate\">get_absolute_url()</code> の注意事項<a class=\"heading-anchor\" href=\"#taking-care-in-get-absolute-url\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>URL は ASCII 文字のみを含むことができます。ASCII でないデータの断片から URL を構築している場合は、URL に適した方法で結果をエンコードすることに注意してください。 <a class=\"reference internal\" href=\"/ja/6.0/ref/urlresolvers/#django.urls.reverse\" title=\"django.urls.reverse\"><code class=\"xref py py-func docutils literal notranslate\">reverse()</code></a> 関数は、これを自動的に処理してくれます。</p>\n<p>URL を手動で構築している場合（つまり、 <code class=\"docutils literal notranslate\">reverse()</code> 関数を <em>使用していない</em> 場合）、エンコーディングを自分で処理する必要があります。この場合、 <a class=\"reference internal\" href=\"#id1\">above</a> で説明された <code class=\"docutils literal notranslate\">iri_to_uri()</code> 関数と <code class=\"docutils literal notranslate\">quote()</code> 関数を使用してください。例えば：</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=\"nn\">urllib.parse</span> <span class=\"kn\">import</span> quote\n<span class=\"kn\">from</span> <span class=\"nn\">django.utils.encoding</span> <span class=\"kn\">import</span> iri_to_uri\n\n\n<span class=\"k\">def</span> <span class=\"nf\">get_absolute_url</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    url <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> quote<span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span>location<span class=\"p\">)</span>\n    <span class=\"k\">return</span> iri_to_uri<span class=\"p\">(</span>url<span class=\"p\">)</span>\n</code></pre></div>\n<p>この関数は、 <code class=\"docutils literal notranslate\">self.location</code> が &quot;Jack visited Paris &amp; Orléans&quot; のようなものであっても、正しくエンコードされた URL を返します。（実際には、上記の例では <code class=\"docutils literal notranslate\">iri_to_uri()</code> 呼び出しは厳密には必要ありません。なぜなら、最初の行での引用符によってすべての非 ASCII 文字が削除されていたからです。）</p>\n</section>\n</section>\n<section id=\"templates\">\n<h2>テンプレート<a class=\"heading-anchor\" href=\"#templates\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>手動でテンプレートを作成する際には文字列を使用します：</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=\"nn\">django.template</span> <span class=\"kn\">import</span> Template\n\nt2 <span class=\"o\">=</span> Template<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=\"/ja/6.0/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\">TEMPLATES</code></a>\nsetting. The built-in <a class=\"reference internal\" href=\"/ja/6.0/topics/templates/#module-django.template.backends.django\" title=\"django.template.backends.django\"><code class=\"xref py py-mod docutils literal notranslate\">django</code></a> backend\nprovides the <code class=\"docutils literal notranslate\">'file_charset'</code> option to change the encoding used to read\nfiles from disk.</p>\n<p><a class=\"reference internal\" href=\"/ja/6.0/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_CHARSET</code></a> 設定は、レンダリングされたテンプレートのエンコーディングを制御します。これはデフォルトで UTF-8 に設定されています。</p>\n<section id=\"template-tags-and-filters\">\n<h3>テンプレートタグとフィルタ<a class=\"heading-anchor\" href=\"#template-tags-and-filters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>自分自身のテンプレートタグとフィルタを書く際に覚えておくべきヒントをいくつか紹介します:</p>\n<ul class=\"simple\">\n<li><p>テンプレートタグの <code class=\"docutils literal notranslate\">render()</code> メソッドとテンプレートフィルタからは、常に文字列を返してください。</p></li>\n<li><p>これらの場所では、<code class=\"docutils literal notranslate\">smart_str()</code> よりも <code class=\"docutils literal notranslate\">force_str()</code> を優先して使用してください。タグのレンダリングとフィルタの呼び出しは、テンプレートがレンダリングされている間に発生するため、遅延翻訳オブジェクトを文字列に変換することを遅らせる利点はありません。その時点で文字列のみを扱う方が簡単です。</p></li>\n</ul>\n</section>\n</section>\n<section id=\"files\">\n<span id=\"unicode-files\"></span><h2>ファイル<a class=\"heading-anchor\" href=\"#files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>ユーザーにファイルのアップロードを許可するつもりであれば、Django を実行する環境が非 ASCII ファイル名に対応しているよう設定する必要があります。環境が正しく設定されていない場合、非 ASCII 文字を含むファイル名や内容のファイルを保存しようとしたときに <code class=\"docutils literal notranslate\">UnicodeEncodeError</code> 例外に遭遇します。</p>\n<p>UTF-8ファイル名に対するファイルシステムのサポートは変わることがあり、環境に依存する場合があります。現在の設定をインタラクティブなPythonシェルで確認するには、次のコマンドを実行してください：</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=\"nn\">sys</span>\n\nsys<span class=\"o\">.</span>getfilesystemencoding<span class=\"p\">()</span>\n</code></pre></div>\n<p>これは &quot;UTF-8&quot; と出力されます。</p>\n<p><code class=\"docutils literal notranslate\">LANG</code> 環境変数は、Unixプラットフォームにおいて予期されるエンコーディングを設定する責任があります。この変数を設定する適切な構文と場所については、使用するオペレーティングシステムとアプリケーションサーバーのドキュメントを参照してください。例については、 <a class=\"reference internal\" href=\"/ja/6.0/howto/deployment/wsgi/modwsgi/\"><span class=\"doc\">Django を Apache と mod_wsgi とともに使うには？</span></a> を参照してください。</p>\n<p>開発環境では、<code class=\"docutils literal notranslate\">~/.bashrc</code> に次のような設定を追加する必要があるかもしれません。</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=\"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>フォームの送信<a class=\"heading-anchor\" href=\"#form-submission\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>HTML フォームの送信は、難しい領域です。送信がエンコーディング情報を含むとは限らず、それはフレームワークが送信されたデータのエンコーディングを推測しなければならないことを意味します。</p>\n<p>Djangoはフォームデータのデコードに対して「遅延」アプローチを採用しています。<code class=\"docutils literal notranslate\">HttpRequest</code> オブジェクト内のデータは、アクセスしたときにのみデコードされます。実際には、ほとんどのデータはデコードされません。<code class=\"docutils literal notranslate\">HttpRequest.GET</code> と <code class=\"docutils literal notranslate\">HttpRequest.POST</code> データ構造のみがデコードが適用されます。これら二つのフィールドは、メンバーをUnicodeデータとして返します。<code class=\"docutils literal notranslate\">HttpRequest</code> の他の属性やメソッドは、クライアントによって送信されたデータをそのまま正確に返します。</p>\n<p>デフォルトでは、 <a class=\"reference internal\" href=\"/ja/6.0/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_CHARSET</code></a> 設定がフォームデータの想定されるエンコーディングとして使用されます。特定のフォームに対してこれを変更する必要がある場合は、 <code class=\"docutils literal notranslate\">HttpRequest</code> インスタンスの <code class=\"docutils literal notranslate\">encoding</code> 属性を設定できます。例えば：</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=\"nf\">some_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"c1\"># We know that the data must be encoded as KOI8-R (for some reason).</span>\n    request<span class=\"o\">.</span>encoding <span class=\"o\">=</span> <span class=\"s2\">&quot;koi8-r&quot;</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">request.GET</code> や <code class=\"docutils literal notranslate\">request.POST</code> にアクセスした後であっても、エンコーディングを変更することができ、以降のすべてのアクセスでは新しいエンコーディングが使用されます。</p>\n<p>ほとんどの開発者はフォームのエンコーディングを変更することを心配する必要はありませんが、制御できないレガシーシステムと通信するアプリケーションにとって、これは便利な機能です。</p>\n<p>Django はファイルアップロードのデータをデコードしません。そのデータは通常、文字列ではなくバイトのコレクションとして扱われるからです。そこでの自動デコードは、バイトストリームの意味を変更してしまうでしょう。</p>\n</section>","rootId":"unicode-data","toc":[{"title":"データベースの作成","anchor":"creating-the-database","children":[]},{"title":"一般的な文字列操作","anchor":"general-string-handling","children":[{"title":"翻訳された文字列","anchor":"translated-strings","children":[]},{"title":"便利なユーティリティ関数","anchor":"useful-utility-functions","children":[{"title":"変換関数","anchor":"conversion-functions","children":[]},{"title":"URI と IRI の処理","anchor":"uri-and-iri-handling","children":[]}]}]},{"title":"モデル","anchor":"models","children":[{"title":"get_absolute_url() の注意事項","anchor":"taking-care-in-get-absolute-url","children":[]}]},{"title":"テンプレート","anchor":"templates","children":[{"title":"テンプレートタグとフィルタ","anchor":"template-tags-and-filters","children":[]}]},{"title":"ファイル","anchor":"files","children":[]},{"title":"フォームの送信","anchor":"form-submission","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API リファレンス","url":"/ja/6.0/ref/"}],"prev":{"docname":"ref/template-response","title":"TemplateResponse および SimpleTemplateResponse","url":"/ja/6.0/ref/template-response/"},"next":{"docname":"ref/urlresolvers","title":"django.urls ユーティリティ関数","url":"/ja/6.0/ref/urlresolvers/"},"formats":{"html":"/ja/6.0/ref/unicode/","markdown":"/ja/6.0/ref/unicode.md","json":"/ja/6.0/ref/unicode.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/ref/unicode.txt","official":"https://docs.djangoproject.com/ja/6.0/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","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}