{"title":"エラーレポートの管理","version":"5.0","locale":"ja","docname":"howto/error-reporting","url":"/ja/5.0/howto/error-reporting/","canonical":"https://djangodocs.dev/ja/5.0/howto/error-reporting/","summary":"サイトを公開している場合、DEBUG 設定は常にオフにしておかなければなりません。これにより、サーバー動作は高速になり、エラーページによって明らかになるアプリケー…","html":"<h1>エラーレポートの管理<a class=\"heading-anchor\" href=\"#how-to-manage-error-reporting\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>サイトを公開している場合、<a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> 設定は常にオフにしておかなければなりません。これにより、サーバー動作は高速になり、エラーページによって明らかになるアプリケーションの詳細が悪意あるユーザーに漏れるのを防ぎます。</p>\n<p>しかし、 <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> を <code class=\"docutils literal notranslate\">False</code> にセットして実行すると、サイトによって生成されるエラーが見られなくなります――表示されるのは万人向けのエラーページだけです。そこで、本番サイトで起きたエラーを追跡するために、Django ではこれらのエラーに関する詳細なレポートを作成するように設定できます。</p>\n<section id=\"email-reports\">\n<h2>E メールのレポート<a class=\"heading-anchor\" href=\"#email-reports\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"server-errors\">\n<h3>サーバーエラー<a class=\"heading-anchor\" href=\"#server-errors\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> が <code class=\"docutils literal notranslate\">False</code> であるとき、コードがハンドリングされない例外を起こして内部サーバーエラーが起きると (厳密には、500 以上の HTTP ステータスコードを持つ全てのレスポンスが返ってくると)、Django は <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-ADMINS\"><code class=\"xref std std-setting docutils literal notranslate\">ADMINS</code></a> 設定のリストに含まれるユーザーにメールを送信します。これにより、管理者はすぐにエラーの通知を受け取ることができます。メールには、<a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-ADMINS\"><code class=\"xref std std-setting docutils literal notranslate\">ADMINS</code></a> はエラーの説明、Python のトレースバック全体、エラーを引き起こした HTTP リクエストの詳細について書かれています。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>メールを送信するために、Django はメールサーバーへの接続方法をいくつか設定する必要があります。最低限 <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-EMAIL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_HOST</code></a> と、おそらく <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-EMAIL_HOST_USER\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_HOST_USER</code></a> と <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-EMAIL_HOST_PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_HOST_PASSWORD</code></a> を指定する必要がありますが、メールサーバーの設定によっては他の設定も必要かもしれません。メール関連の設定の一覧は <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/\"><span class=\"doc\">Django の設定のドキュメント</span></a> を参照してください。</p>\n</aside>\n<p>デフォルトでは、 Django は <a class=\"reference external\" href=\"mailto:root&#37;&#52;&#48;localhost\">root<span>&#64;</span>localhost</a> からメールを送信します。しかし、メールプロバイダによっては、このアドレスからのメールを全て拒否します。別の送信者アドレスを使うには、 <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-SERVER_EMAIL\"><code class=\"xref std std-setting docutils literal notranslate\">SERVER_EMAIL</code></a> 設定を変更してください。</p>\n<p>この動作を有効にするには、受信者のメールアドレスを <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-ADMINS\"><code class=\"xref std std-setting docutils literal notranslate\">ADMINS</code></a> 設定に含めます。</p>\n<aside class=\"admonition admonition-seealso\" role=\"note\">\n<p class=\"admonition-title\">参考</p>\n<p>サーバーエラーのメールはロギングフレームワークを使って送信されるので、 <a class=\"reference internal\" href=\"/ja/5.0/topics/logging/\"><span class=\"doc\">ロギングの設定をカスタマイズする</span></a> ことで動作を変更できます。</p>\n</aside>\n</section>\n<section id=\"errors\">\n<h3>404 エラー<a class=\"heading-anchor\" href=\"#errors\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django は、リンク切れに関するエラー (404 &quot;page not found&quot; エラー) をメールで送信するように設定することもできます。Django は以下の場合に 404 エラーに関するメールを送ります:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> が <code class=\"docutils literal notranslate\">False</code> のとき;</p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\">MIDDLEWARE</code></a> 設定に <a class=\"reference internal\" href=\"/ja/5.0/ref/middleware/#django.middleware.common.BrokenLinkEmailsMiddleware\" title=\"django.middleware.common.BrokenLinkEmailsMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">django.middleware.common.BrokenLinkEmailsMiddleware</code></a> が含まれているとき。</p></li>\n</ul>\n<p>これらの条件が満たされると、Django はあなたのコードが 404 を発生させ、そのリクエストがリファラを持っている場合、 <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-MANAGERS\"><code class=\"xref std std-setting docutils literal notranslate\">MANAGERS</code></a> 設定にリストされているユーザにメールを送ります。リファラを持たない 404 にはわざわざメールを送りません。それは大抵、壊れた URL を入力したユーザか、壊れた Web ボットです。また、リファラがリクエストされた URL と同じ場合の 404 も無視します。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p><a class=\"reference internal\" href=\"/ja/5.0/ref/middleware/#django.middleware.common.BrokenLinkEmailsMiddleware\" title=\"django.middleware.common.BrokenLinkEmailsMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">BrokenLinkEmailsMiddleware</code></a> は、 <a class=\"reference internal\" href=\"/ja/5.0/ref/middleware/#django.middleware.locale.LocaleMiddleware\" title=\"django.middleware.locale.LocaleMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">LocaleMiddleware</code></a> や <a class=\"reference internal\" href=\"/ja/5.0/ref/contrib/flatpages/#django.contrib.flatpages.middleware.FlatpageFallbackMiddleware\" title=\"django.contrib.flatpages.middleware.FlatpageFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">FlatpageFallbackMiddleware</code></a> のような 404 エラーを検出する他のミドルウェアよりも前に表示する必要があります。これを <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\">MIDDLEWARE</code></a> 設定の一番上に置いてください。</p>\n</aside>\n<p>特定の 404 を報告しないように Django に指示するには、 <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-IGNORABLE_404_URLS\"><code class=\"xref std std-setting docutils literal notranslate\">IGNORABLE_404_URLS</code></a> 設定をいじります。これはコンパイルされた正規表現オブジェクトのリストでなければなりません。例えば:</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\">re</span>\n\nIGNORABLE_404_URLS <span class=\"o\">=</span> <span class=\"p\">[</span>\n    re<span class=\"o\">.</span>compile<span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s2\">&quot;\\.(php|cgi)$&quot;</span><span class=\"p\">),</span>\n    re<span class=\"o\">.</span>compile<span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s2\">&quot;^/phpmyadmin/&quot;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>この例では、 <code class=\"docutils literal notranslate\">.php</code> または <code class=\"docutils literal notranslate\">.cgi</code> で終わる URL への 404 は報告されません。また、 <code class=\"docutils literal notranslate\">/phpmyadmin/</code> で始まるURLも報告されません。</p>\n<p>以下は、ブラウザやクローラがよくリクエストする従来の URL を除外する方法の例です:</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\">re</span>\n\nIGNORABLE_404_URLS <span class=\"o\">=</span> <span class=\"p\">[</span>\n    re<span class=\"o\">.</span>compile<span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s2\">&quot;^/apple-touch-icon.*\\.png$&quot;</span><span class=\"p\">),</span>\n    re<span class=\"o\">.</span>compile<span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s2\">&quot;^/favicon\\.ico$&quot;</span><span class=\"p\">),</span>\n    re<span class=\"o\">.</span>compile<span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s2\">&quot;^/robots\\.txt$&quot;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>(これらは正規表現なので、ピリオドの前にバックスラッシュを入れてエスケープしていることに注意してください)</p>\n<p><a class=\"reference internal\" href=\"/ja/5.0/ref/middleware/#django.middleware.common.BrokenLinkEmailsMiddleware\" title=\"django.middleware.common.BrokenLinkEmailsMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">django.middleware.common.BrokenLinkEmailsMiddleware</code></a> の動作をさらにカスタマイズしたい場合(例えば、ウェブクローラからのリクエストを無視したい場合など)は、サブクラスを作成し、そのメソッドをオーバーライドしてください。</p>\n<aside class=\"admonition admonition-seealso\" role=\"note\">\n<p class=\"admonition-title\">参考</p>\n<p>404エラーはロギングフレームワークを使って記録されます。デフォルトではこれらのログレコードは無視されますが、ハンドラを書いて <a class=\"reference internal\" href=\"/ja/5.0/topics/logging/\"><span class=\"doc\">ロギングを適切に設定する</span></a> ことで、エラー報告に使うことができます。</p>\n</aside>\n</section>\n</section>\n<section id=\"filtering-error-reports\">\n<span id=\"id1\"></span><h2>エラーレポートをフィルタリングする<a class=\"heading-anchor\" href=\"#filtering-error-reports\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>機密データのフィルタリングは難しい問題であり、機密データがエラーレポートに漏れないことを保証することはほとんど不可能です。したがって、エラーレポートは信頼できるチームメンバーのみが利用できるようにし、インターネット上で暗号化されていないエラーレポートを送信することは避けるべきです（電子メールなど）。</p>\n</aside>\n<section id=\"filtering-sensitive-information\">\n<h3>機密情報のフィルタリング<a class=\"heading-anchor\" href=\"#filtering-sensitive-information\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>エラーレポートはエラーのデバッグにとても役立つので、通常は、エラーに関する関連情報をできるだけ多く記録しておくと便利です。例えば、デフォルトでは Django は発生した例外の <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Stack_trace\">full traceback</a> や、各 <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Stack_frame\">traceback frame</a> のローカル変数、 <a class=\"reference internal\" href=\"/ja/5.0/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\">HttpRequest</code></a> の <a class=\"reference internal\" href=\"/ja/5.0/ref/request-response/#httprequest-attributes\"><span class=\"std std-ref\">属性</span></a> を記録します。</p>\n<p>しかし、例えばユーザのパスワードやクレジットカード番号のように、ある種の情報は機密性が高すぎるため、記録するのが適切でない場合があります。そこで Django では、 <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> ドキュメントで説明されているように、機密性が高そうな設定をフィルタリングすることに加えて、本番環境 (つまり、 <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> が <code class=\"docutils literal notranslate\">False</code> に設定されている場合) でエラーレポートからフィルタリングされる情報を制御するのに役立つ関数デコレータを提供しています。 <a class=\"reference internal\" href=\"#django.views.decorators.debug.sensitive_variables\" title=\"django.views.decorators.debug.sensitive_variables\"><code class=\"xref py py-func docutils literal notranslate\">sensitive_variables()</code></a> と <a class=\"reference internal\" href=\"#django.views.decorators.debug.sensitive_post_parameters\" title=\"django.views.decorators.debug.sensitive_post_parameters\"><code class=\"xref py py-func docutils literal notranslate\">sensitive_post_parameters()</code></a> です。</p>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.decorators.debug.sensitive_variables\">\n<span class=\"sig-name descname\">sensitive_variables</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>variables</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.decorators.debug.sensitive_variables\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>コード内の関数（ビューまたは通常のコールバック）が機密情報を含む可能性のあるローカル変数を使用している場合、 <code class=\"docutils literal notranslate\">sensitive_variables</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\">django.views.decorators.debug</span> <span class=\"kn\">import</span> sensitive_variables\n\n\n<span class=\"nd\">@sensitive_variables</span><span class=\"p\">(</span><span class=\"s2\">&quot;user&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;pw&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;cc&quot;</span><span class=\"p\">)</span>\n<span class=\"k\">def</span> <span class=\"nf\">process_info</span><span class=\"p\">(</span>user<span class=\"p\">):</span>\n    pw <span class=\"o\">=</span> user<span class=\"o\">.</span>pass_word\n    cc <span class=\"o\">=</span> user<span class=\"o\">.</span>credit_card_number\n    name <span class=\"o\">=</span> user<span class=\"o\">.</span>name\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>上記の例では、 <code class=\"docutils literal notranslate\">user</code>、<code class=\"docutils literal notranslate\">pw</code>、<code class=\"docutils literal notranslate\">cc</code> の値は非表示になり、エラーレポートではアスタリスク (<code class=\"docutils literal notranslate\">*******</code>) に置き換えられますが、 <code class=\"docutils literal notranslate\">name</code> 変数の値は公開されます。</p>\n<p>関数のすべてのローカル変数をエラーログから徹底的に隠す場合は、引数なしで <code class=\"docutils literal notranslate\">sensitive_variables</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=\"nd\">@sensitive_variables</span><span class=\"p\">()</span>\n<span class=\"k\">def</span> <span class=\"nf\">my_function</span><span class=\"p\">():</span> <span class=\"o\">...</span>\n</code></pre></div>\n<aside class=\"admonition-when-using-multiple-decorators admonition\" role=\"note\">\n<p class=\"admonition-title\">複数のデコレータを使うとき</p>\n<p>非表示にしたい変数が関数の引数でもある場合 (例えば、次の例では '<code class=\"docutils literal notranslate\">user</code>') 、そしてデコレートされた関数に複数のデコレーターがある場合は、 <code class=\"docutils literal notranslate\">&#64;sensitive_variables</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=\"nd\">@sensitive_variables</span><span class=\"p\">(</span><span class=\"s2\">&quot;user&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;pw&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;cc&quot;</span><span class=\"p\">)</span>\n<span class=\"nd\">@some_decorator</span>\n<span class=\"nd\">@another_decorator</span>\n<span class=\"k\">def</span> <span class=\"nf\">process_info</span><span class=\"p\">(</span>user<span class=\"p\">):</span> <span class=\"o\">...</span>\n</code></pre></div>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"5.0\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 5.0</p><p><code class=\"docutils literal notranslate\">async</code> 関数のラップをサポートしました。</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.decorators.debug.sensitive_post_parameters\">\n<span class=\"sig-name descname\">sensitive_post_parameters</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>parameters</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.decorators.debug.sensitive_post_parameters\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>もしビューの 1 つが <a class=\"reference internal\" href=\"/ja/5.0/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\">HttpRequest</code></a> オブジェクトを <a class=\"reference internal\" href=\"/ja/5.0/ref/request-response/#django.http.HttpRequest.POST\" title=\"django.http.HttpRequest.POST\"><code class=\"xref py py-attr docutils literal notranslate\">POST parameters</code></a> で受け取った場合、機密情報が含まれている可能性があるので、 <code class=\"docutils literal notranslate\">sensitive_post_parameters</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\">django.views.decorators.debug</span> <span class=\"kn\">import</span> sensitive_post_parameters\n\n\n<span class=\"nd\">@sensitive_post_parameters</span><span class=\"p\">(</span><span class=\"s2\">&quot;pass_word&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;credit_card_number&quot;</span><span class=\"p\">)</span>\n<span class=\"k\">def</span> <span class=\"nf\">record_user_profile</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    UserProfile<span class=\"o\">.</span>create<span class=\"p\">(</span>\n        user<span class=\"o\">=</span>request<span class=\"o\">.</span>user<span class=\"p\">,</span>\n        password<span class=\"o\">=</span>request<span class=\"o\">.</span>POST<span class=\"p\">[</span><span class=\"s2\">&quot;pass_word&quot;</span><span class=\"p\">],</span>\n        credit_card<span class=\"o\">=</span>request<span class=\"o\">.</span>POST<span class=\"p\">[</span><span class=\"s2\">&quot;credit_card_number&quot;</span><span class=\"p\">],</span>\n        name<span class=\"o\">=</span>request<span class=\"o\">.</span>POST<span class=\"p\">[</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">],</span>\n    <span class=\"p\">)</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>上の例では、 <code class=\"docutils literal notranslate\">pass_word</code> と <code class=\"docutils literal notranslate\">credit_card_number</code> のPOSTパラメータの値は隠され、エラーレポート内のリクエストの表現ではアスタリスク (<code class=\"docutils literal notranslate\">*******</code>) に置き換えられますが、 <code class=\"docutils literal notranslate\">name</code> パラメータの値は公開されます。</p>\n<p>エラーレポートでリクエストの全てのPOSTパラメータを徹底的に隠す場合は、 引数なしで <code class=\"docutils literal notranslate\">sensitive_post_parameters</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=\"nd\">@sensitive_post_parameters</span><span class=\"p\">()</span>\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span> <span class=\"o\">...</span>\n</code></pre></div>\n<p>特定の <a class=\"reference internal\" href=\"/ja/5.0/topics/auth/default/#module-django.contrib.auth.views\" title=\"django.contrib.auth.views\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.auth.views</code></a> のビュー (<code class=\"docutils literal notranslate\">auth</code> admin 内の <code class=\"docutils literal notranslate\">login</code>, <code class=\"docutils literal notranslate\">password_reset_confirm</code>, <code class=\"docutils literal notranslate\">password_change</code>, <code class=\"docutils literal notranslate\">auth</code> admin の <code class=\"docutils literal notranslate\">add_view</code> と <code class=\"docutils literal notranslate\">user_change_password</code>) のエラーレポートから、すべての POST パラメータが徹底的にフィルタされ、ユーザパスワードのような機密情報の漏洩を防ぎます。</p>\n<aside class=\"version-note version-changed\" data-version=\"5.0\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 5.0</p><p><code class=\"docutils literal notranslate\">async</code> 関数のラップをサポートしました。</p>\n</aside>\n</dd></dl>\n\n</section>\n<section id=\"custom-error-reports\">\n<span id=\"id2\"></span><h3>カスタムエラーレポート<a class=\"heading-anchor\" href=\"#custom-error-reports\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference internal\" href=\"#django.views.decorators.debug.sensitive_variables\" title=\"django.views.decorators.debug.sensitive_variables\"><code class=\"xref py py-func docutils literal notranslate\">sensitive_variables()</code></a> と <a class=\"reference internal\" href=\"#django.views.decorators.debug.sensitive_post_parameters\" title=\"django.views.decorators.debug.sensitive_post_parameters\"><code class=\"xref py py-func docutils literal notranslate\">sensitive_post_parameters()</code></a> がすることは、それぞれ、装飾された関数にセンシティブな変数名をアノテートし、<code class=\"docutils literal notranslate\">HttpRequest</code> オブジェクトにセンシティブな POST パラメータ名をアノテートするだけです。実際のフィルタリングは Django のデフォルトのエラーレポートフィルタ ( <a class=\"reference internal\" href=\"#django.views.debug.SafeExceptionReporterFilter\" title=\"django.views.debug.SafeExceptionReporterFilter\"><code class=\"xref py py-class docutils literal notranslate\">django.views.debug.SafeExceptionReporterFilter</code></a> ) で行われます。このフィルタはデコレータのアノテーションを使い、エラーレポートが生成される際に、対応する値をアスタリスク (<code class=\"docutils literal notranslate\">*******</code>) に置き換えます。このデフォルトの動作を上書きしたり、サイト全体でカスタマイズしたい場合は、独自のフィルタクラスを定義し、 <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-DEFAULT_EXCEPTION_REPORTER_FILTER\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_EXCEPTION_REPORTER_FILTER</code></a> 設定でそれを使うように Django に指示する必要があります:</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>DEFAULT_EXCEPTION_REPORTER_FILTER <span class=\"o\">=</span> <span class=\"s2\">&quot;path.to.your.CustomExceptionReporterFilter&quot;</span>\n</code></pre></div>\n<p>また、 <code class=\"docutils literal notranslate\">HttpRequest</code> の <code class=\"docutils literal notranslate\">exception_reporter_filter</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\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"k\">if</span> request<span class=\"o\">.</span>user<span class=\"o\">.</span>is_authenticated<span class=\"p\">:</span>\n        request<span class=\"o\">.</span>exception_reporter_filter <span class=\"o\">=</span> CustomExceptionReporterFilter<span class=\"p\">()</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>カスタムフィルタクラスは <a class=\"reference internal\" href=\"#django.views.debug.SafeExceptionReporterFilter\" title=\"django.views.debug.SafeExceptionReporterFilter\"><code class=\"xref py py-class docutils literal notranslate\">django.views.debug.SafeExceptionReporterFilter</code></a> を継承し、以下の属性とメソッドをオーバーライドする必要があります:</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.views.debug.SafeExceptionReporterFilter\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">SafeExceptionReporterFilter</span><a class=\"heading-anchor\" href=\"#django.views.debug.SafeExceptionReporterFilter\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.views.debug.SafeExceptionReporterFilter.cleansed_substitute\">\n<span class=\"sig-name descname\">cleansed_substitute</span><a class=\"heading-anchor\" href=\"#django.views.debug.SafeExceptionReporterFilter.cleansed_substitute\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>センシティブな値を置換する文字列値。デフォルトでは、センシティブな変数の値をアスタリスク (<code class=\"docutils literal notranslate\">*******</code>) で置き換えます。</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.views.debug.SafeExceptionReporterFilter.hidden_settings\">\n<span class=\"sig-name descname\">hidden_settings</span><a class=\"heading-anchor\" href=\"#django.views.debug.SafeExceptionReporterFilter.hidden_settings\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>センシティブな設定や <code class=\"docutils literal notranslate\">request.META</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\">import</span> <span class=\"nn\">re</span>\n\nre<span class=\"o\">.</span>compile<span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s2\">&quot;API|TOKEN|KEY|SECRET|PASS|SIGNATURE|HTTP_COOKIE&quot;</span><span class=\"p\">,</span> flags<span class=\"o\">=</span>re<span class=\"o\">.</span>IGNORECASE<span class=\"p\">)</span>\n</code></pre></div>\n<aside class=\"version-note version-changed\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 4.2</p><p><code class=\"docutils literal notranslate\">HTTP_COOKIE</code> が追加されました。</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.views.debug.SafeExceptionReporterFilter.is_active\">\n<span class=\"sig-name descname\">is_active</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.debug.SafeExceptionReporterFilter.is_active\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><a class=\"reference internal\" href=\"#django.views.debug.SafeExceptionReporterFilter.get_post_parameters\" title=\"django.views.debug.SafeExceptionReporterFilter.get_post_parameters\"><code class=\"xref py py-meth docutils literal notranslate\">get_post_parameters()</code></a> と <a class=\"reference internal\" href=\"#django.views.debug.SafeExceptionReporterFilter.get_traceback_frame_variables\" title=\"django.views.debug.SafeExceptionReporterFilter.get_traceback_frame_variables\"><code class=\"xref py py-meth docutils literal notranslate\">get_traceback_frame_variables()</code></a> のフィルタリングを有効にする場合は <code class=\"docutils literal notranslate\">True</code> を返します。デフォルトでは <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> が <code class=\"docutils literal notranslate\">False</code> の場合に有効になります。 <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> のドキュメントにあるように、センシティブな <code class=\"docutils literal notranslate\">request.META</code> の値は常に、センシティブな設定値とともにフィルタリングされることに注意してください。</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.views.debug.SafeExceptionReporterFilter.get_post_parameters\">\n<span class=\"sig-name descname\">get_post_parameters</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.debug.SafeExceptionReporterFilter.get_post_parameters\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>フィルタリングされた POST パラメータの辞書を返します。センシティブな値は <a class=\"reference internal\" href=\"#django.views.debug.SafeExceptionReporterFilter.cleansed_substitute\" title=\"django.views.debug.SafeExceptionReporterFilter.cleansed_substitute\"><code class=\"xref py py-attr docutils literal notranslate\">cleansed_substitute</code></a> で置換されます。</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.views.debug.SafeExceptionReporterFilter.get_traceback_frame_variables\">\n<span class=\"sig-name descname\">get_traceback_frame_variables</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em>, <em class=\"sig-param\">tb_frame</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.debug.SafeExceptionReporterFilter.get_traceback_frame_variables\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>与えられたトレースバックフレームのローカル変数のフィルタリングされた辞書を返します。センシティブな値は <a class=\"reference internal\" href=\"#django.views.debug.SafeExceptionReporterFilter.cleansed_substitute\" title=\"django.views.debug.SafeExceptionReporterFilter.cleansed_substitute\"><code class=\"xref py py-attr docutils literal notranslate\">cleansed_substitute</code></a> で置き換えられます。</p>\n</dd></dl>\n\n</dd></dl>\n\n<p>フィルタリングの他にエラーレポートをカスタマイズする必要がある場合は、 <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-DEFAULT_EXCEPTION_REPORTER\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_EXCEPTION_REPORTER</code></a> 設定を定義することでカスタムエラーレポータークラスを指定できます:</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>DEFAULT_EXCEPTION_REPORTER <span class=\"o\">=</span> <span class=\"s2\">&quot;path.to.your.CustomExceptionReporter&quot;</span>\n</code></pre></div>\n<p>例外レポーターは例外レポートデータをコンパイルし、テキストまたはHTMLとして適切にフォーマットする責任があります。(例外報告レポーターは、例外レポートデータを作成する際に <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-DEFAULT_EXCEPTION_REPORTER_FILTER\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_EXCEPTION_REPORTER_FILTER</code></a> を使用します)。</p>\n<p>カスタムレポータークラスは <a class=\"reference internal\" href=\"#django.views.debug.ExceptionReporter\" title=\"django.views.debug.ExceptionReporter\"><code class=\"xref py py-class docutils literal notranslate\">django.views.debug.ExceptionReporter</code></a> を継承する必要があります。</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.views.debug.ExceptionReporter\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">ExceptionReporter</span><a class=\"heading-anchor\" href=\"#django.views.debug.ExceptionReporter\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.views.debug.ExceptionReporter.html_template_path\">\n<span class=\"sig-name descname\">html_template_path</span><a class=\"heading-anchor\" href=\"#django.views.debug.ExceptionReporter.html_template_path\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>例外の HTML 表現をレンダリングするテンプレートへの絶対ファイルシステムパスを表す <a class=\"reference external\" href=\"https://docs.python.org/3/library/pathlib.html#pathlib.Path\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">pathlib.Path</code></a> を返すプロパティです。デフォルトは Django が提供するテンプレートです。</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.views.debug.ExceptionReporter.text_template_path\">\n<span class=\"sig-name descname\">text_template_path</span><a class=\"heading-anchor\" href=\"#django.views.debug.ExceptionReporter.text_template_path\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><a class=\"reference external\" href=\"https://docs.python.org/3/library/pathlib.html#pathlib.Path\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">pathlib.Path</code></a> を返すプロパティで、例外のプレーンテキスト表現をレンダリングするテンプレー トへのファイルシステムの絶対パスを表します。デフォルトは Django が提供するテンプレートです。</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.views.debug.ExceptionReporter.get_traceback_data\">\n<span class=\"sig-name descname\">get_traceback_data</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.debug.ExceptionReporter.get_traceback_data\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>トレースバック情報を含む辞書を返します。</p>\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.views.debug</span> <span class=\"kn\">import</span> ExceptionReporter\n\n\n<span class=\"k\">class</span> <span class=\"nc\">CustomExceptionReporter</span><span class=\"p\">(</span>ExceptionReporter<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">get_traceback_data</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        data <span class=\"o\">=</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>get_traceback_data<span class=\"p\">()</span>\n        <span class=\"c1\"># ... remove/add something here ...</span>\n        <span class=\"k\">return</span> data\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.views.debug.ExceptionReporter.get_traceback_html\">\n<span class=\"sig-name descname\">get_traceback_html</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.debug.ExceptionReporter.get_traceback_html\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>例外レポートの HTML 版を返します。</p>\n<p>デバッグ 500 HTTP エラー・ページの HTML 版に使用されます。</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.views.debug.ExceptionReporter.get_traceback_text\">\n<span class=\"sig-name descname\">get_traceback_text</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.debug.ExceptionReporter.get_traceback_text\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>例外レポートのプレーンテキスト版を返します。</p>\n<p>デバッグ 500 HTTP エラーページおよび E メールレポートのプレーンテキスト版に使用されます。</p>\n</dd></dl>\n\n</dd></dl>\n\n<p>フィルタクラスと同様に、 <code class=\"docutils literal notranslate\">HttpRequest</code> の <code class=\"docutils literal notranslate\">exception_reporter_class</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\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"k\">if</span> request<span class=\"o\">.</span>user<span class=\"o\">.</span>is_authenticated<span class=\"p\">:</span>\n        request<span class=\"o\">.</span>exception_reporter_class <span class=\"o\">=</span> CustomExceptionReporter<span class=\"p\">()</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n<aside class=\"admonition admonition-seealso\" role=\"note\">\n<p class=\"admonition-title\">参考</p>\n<p>また、独自の <a class=\"reference internal\" href=\"/ja/5.0/topics/http/middleware/#exception-middleware\"><span class=\"std std-ref\">例外ミドルウェア</span></a> を書くことで、独自のエラーレポートを設定することもできます。もしカスタムエラー処理を書くのであれば、 Django の組み込みエラー処理を真似て、 <a class=\"reference internal\" href=\"/ja/5.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> が <code class=\"docutils literal notranslate\">False</code> の場合のみエラーを報告/ログ出力するようにすると良いでしょう。</p>\n</aside>\n</section>\n</section>","rootId":"how-to-manage-error-reporting","toc":[{"title":"E メールのレポート","anchor":"email-reports","children":[{"title":"サーバーエラー","anchor":"server-errors","children":[]},{"title":"404 エラー","anchor":"errors","children":[]}]},{"title":"エラーレポートをフィルタリングする","anchor":"filtering-error-reports","children":[{"title":"機密情報のフィルタリング","anchor":"filtering-sensitive-information","children":[]},{"title":"カスタムエラーレポート","anchor":"custom-error-reports","children":[]}]}],"breadcrumbs":[{"docname":"howto/index","title":"「How-to」ガイド","url":"/ja/5.0/howto/"}],"prev":{"docname":"howto/upgrade-version","title":"Django の新しいバージョンへの更新","url":"/ja/5.0/howto/upgrade-version/"},"next":{"docname":"howto/initial-data","title":"モデルに初期データを投入する","url":"/ja/5.0/howto/initial-data/"},"formats":{"html":"/ja/5.0/howto/error-reporting/","markdown":"/ja/5.0/howto/error-reporting.md","json":"/ja/5.0/howto/error-reporting.json"},"source":"https://github.com/django/django/blob/stable/5.0.x/docs/howto/error-reporting.txt","official":"https://docs.djangoproject.com/ja/5.0/howto/error-reporting/","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"]}