{"title":"Built-in Views","version":"3.0","locale":"zh-hans","docname":"ref/views","url":"/zh-hans/3.0/ref/views/","canonical":"https://djangodocs.dev/zh-hans/3.0/ref/views/","summary":"Several of Django's built-in views are documented in 编写视图 as well as elsewhere in the documentation. Serving files in development Link to this heading # static.…","html":"<span id=\"built-in-views\"></span><h1>Built-in Views<a class=\"heading-anchor\" href=\"#module-django.views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Several of Django's built-in views are documented in\n<a class=\"reference internal\" href=\"/zh-hans/3.0/topics/http/views/\"><span class=\"doc\">编写视图</span></a> as well as elsewhere in the documentation.</p>\n<section id=\"serving-files-in-development\">\n<h2>Serving files in development<a class=\"heading-anchor\" href=\"#serving-files-in-development\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.static.serve\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">static.</span></span><span class=\"sig-name descname\"><span class=\"pre\">serve</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">request</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">path</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">document_root</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">show_indexes</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.static.serve\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>There may be files other than your project's static assets that, for\nconvenience, you'd like to have Django serve for you in local development.\nThe <a class=\"reference internal\" href=\"#django.views.static.serve\" title=\"django.views.static.serve\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">serve()</span></code></a> view can be used to serve any directory\nyou give it. (This view is <strong>not</strong> hardened for production use and should be\nused only as a development aid; you should serve these files in production\nusing a real front-end web server).</p>\n<p>The most likely example is user-uploaded content in <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_ROOT</span></code></a>.\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code> is intended for static assets and has no\nbuilt-in handling for user-uploaded files, but you can have Django serve your\n<a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_ROOT</span></code></a> by appending something like this to your URLconf:</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.conf</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">settings</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">re_path</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.views.static</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">serve</span>\n\n<span class=\"c1\"># ... the rest of your URLconf goes here ...</span>\n\n<span class=\"k\">if</span> <span class=\"n\">settings</span><span class=\"o\">.</span><span class=\"n\">DEBUG</span><span class=\"p\">:</span>\n    <span class=\"n\">urlpatterns</span> <span class=\"o\">+=</span> <span class=\"p\">[</span>\n        <span class=\"n\">re_path</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^media/(?P&lt;path&gt;.*)$&#39;</span><span class=\"p\">,</span> <span class=\"n\">serve</span><span class=\"p\">,</span> <span class=\"p\">{</span>\n            <span class=\"s1\">&#39;document_root&#39;</span><span class=\"p\">:</span> <span class=\"n\">settings</span><span class=\"o\">.</span><span class=\"n\">MEDIA_ROOT</span><span class=\"p\">,</span>\n        <span class=\"p\">}),</span>\n    <span class=\"p\">]</span>\n</code></pre></div>\n<p>Note, the snippet assumes your <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code></a> has a value of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'/media/'</span></code>. This will call the <a class=\"reference internal\" href=\"#django.views.static.serve\" title=\"django.views.static.serve\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">serve()</span></code></a> view,\npassing in the path from the URLconf and the (required) <code class=\"docutils literal notranslate\"><span class=\"pre\">document_root</span></code>\nparameter.</p>\n<p>Since it can become a bit cumbersome to define this URL pattern, Django\nships with a small URL helper function <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/urls/#django.conf.urls.static.static\" title=\"django.conf.urls.static.static\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">static()</span></code></a>\nthat takes as parameters the prefix such as <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code></a> and a dotted\npath to a view, such as <code class=\"docutils literal notranslate\"><span class=\"pre\">'django.views.static.serve'</span></code>. Any other function\nparameter will be transparently passed to the view.</p>\n</section>\n<section id=\"error-views\">\n<span id=\"id1\"></span><h2>错误的视图<a class=\"heading-anchor\" href=\"#error-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django comes with a few views by default for handling HTTP errors. To override\nthese with your own custom views, see <a class=\"reference internal\" href=\"/zh-hans/3.0/topics/http/views/#customizing-error-views\"><span class=\"std std-ref\">自定义报错视图</span></a>.</p>\n<section id=\"the-404-page-not-found-view\">\n<span id=\"http-not-found-view\"></span><h3>404 (page not found) 视图<a class=\"heading-anchor\" href=\"#the-404-page-not-found-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.defaults.page_not_found\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">defaults.</span></span><span class=\"sig-name descname\"><span class=\"pre\">page_not_found</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">request</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">exception</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">template_name</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">'404.html'</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.defaults.page_not_found\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>When you raise <a class=\"reference internal\" href=\"/zh-hans/3.0/topics/http/views/#django.http.Http404\" title=\"django.http.Http404\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">Http404</span></code></a> from within a view, Django loads a\nspecial view devoted to handling 404 errors. By default, it's the view\n<a class=\"reference internal\" href=\"#django.views.defaults.page_not_found\" title=\"django.views.defaults.page_not_found\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.views.defaults.page_not_found()</span></code></a>, which either produces a &quot;Not\nFound&quot; message or loads and renders the template <code class=\"docutils literal notranslate\"><span class=\"pre\">404.html</span></code> if you created it\nin your root template directory.</p>\n<p>The default 404 view will pass two variables to the template: <code class=\"docutils literal notranslate\"><span class=\"pre\">request_path</span></code>,\nwhich is the URL that resulted in the error, and <code class=\"docutils literal notranslate\"><span class=\"pre\">exception</span></code>, which is a\nuseful representation of the exception that triggered the view (e.g. containing\nany message passed to a specific <code class=\"docutils literal notranslate\"><span class=\"pre\">Http404</span></code> instance).</p>\n<p>关于404视图需要注意的三个要点：</p>\n<ul class=\"simple\">\n<li><p>The 404 view is also called if Django doesn't find a match after\nchecking every regular expression in the URLconf.</p></li>\n<li><p>The 404 view is passed a <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/templates/api/#django.template.RequestContext\" title=\"django.template.RequestContext\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RequestContext</span></code></a> and\nwill have access to variables supplied by your template context\nprocessors (e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code>).</p></li>\n<li><p>如果 <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a> 设置为``True``（在您的配置模块中），那么您的404视图将永远不会被使用，作为替代，你的URL配置与一些调试信息将会被显示。</p></li>\n</ul>\n</section>\n<section id=\"the-500-server-error-view\">\n<span id=\"http-internal-server-error-view\"></span><h3>The 500 (server error) view<a class=\"heading-anchor\" href=\"#the-500-server-error-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.defaults.server_error\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">defaults.</span></span><span class=\"sig-name descname\"><span class=\"pre\">server_error</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">request</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">template_name</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">'500.html'</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.defaults.server_error\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Similarly, Django executes special-case behavior in the case of runtime errors\nin view code. If a view results in an exception, Django will, by default, call\nthe view <code class=\"docutils literal notranslate\"><span class=\"pre\">django.views.defaults.server_error</span></code>, which either produces a\n&quot;Server Error&quot; message or loads and renders the template <code class=\"docutils literal notranslate\"><span class=\"pre\">500.html</span></code> if you\ncreated it in your root template directory.</p>\n<p>默认的500视图不会将变量传递给``500.html``模板，并使用空的``Context``渲染以减少出现附加错误的可能性。</p>\n<p>如果 <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a> 设置为``True``（在您的配置模块中），那么您的500视图将永远不会被使用，作为替代，调用回溯与一些调试信息将会被显示。</p>\n</section>\n<section id=\"the-403-http-forbidden-view\">\n<span id=\"http-forbidden-view\"></span><h3>403 (HTTP Forbidden) 视图<a class=\"heading-anchor\" href=\"#the-403-http-forbidden-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.defaults.permission_denied\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">defaults.</span></span><span class=\"sig-name descname\"><span class=\"pre\">permission_denied</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">request</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">exception</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">template_name</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">'403.html'</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.defaults.permission_denied\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>与404和500视图一样，Django有处理403 Forbidden错误的视图。如果视图导致403异常，那么Django默认会调用视图``django.views.defaults.permission_denied``。</p>\n<p>这个视图从你的根模板目录加载和渲染 <code class=\"docutils literal notranslate\"><span class=\"pre\">403.html</span></code> 模板，如果这个文件不存在将会显示“403 Forbidden”，按照 <span class=\"target\" id=\"index-2\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc7231.html#section-6.5.3\"><strong>RFC 7231 Section 6.5.3</strong></a> （HTTP 1.1 规范）。 模板上下文包含 <code class=\"docutils literal notranslate\"><span class=\"pre\">exception</span></code>，它是触发视图的异常的字符串表示形式。</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.views.defaults.permission_denied</span></code> is triggered by a\n<a class=\"reference internal\" href=\"/zh-hans/3.0/ref/exceptions/#django.core.exceptions.PermissionDenied\" title=\"django.core.exceptions.PermissionDenied\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">PermissionDenied</span></code></a> exception. To deny access in a\nview you can use code like this:</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.core.exceptions</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">PermissionDenied</span>\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">edit</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"n\">pk</span><span class=\"p\">):</span>\n    <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">user</span><span class=\"o\">.</span><span class=\"n\">is_staff</span><span class=\"p\">:</span>\n        <span class=\"k\">raise</span> <span class=\"n\">PermissionDenied</span>\n    <span class=\"c1\"># ...</span>\n</code></pre></div>\n</section>\n<section id=\"the-400-bad-request-view\">\n<span id=\"http-bad-request-view\"></span><h3>400 (bad request) 视图<a class=\"heading-anchor\" href=\"#the-400-bad-request-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.defaults.bad_request\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">defaults.</span></span><span class=\"sig-name descname\"><span class=\"pre\">bad_request</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">request</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">exception</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">template_name</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">'400.html'</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.defaults.bad_request\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>When a <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/exceptions/#django.core.exceptions.SuspiciousOperation\" title=\"django.core.exceptions.SuspiciousOperation\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">SuspiciousOperation</span></code></a> is raised in Django,\nit may be handled by a component of Django (for example resetting the session\ndata). If not specifically handled, Django will consider the current request a\n'bad request' instead of a server error.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.views.defaults.bad_request</span></code>, is otherwise very similar to the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">server_error</span></code> view, but returns with the status code 400 indicating that\nthe error condition was the result of a client operation. By default, nothing\nrelated to the exception that triggered the view is passed to the template\ncontext, as the exception message might contain sensitive information like\nfilesystem paths.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">bad_request``视图也仅在以下情况下使用</span> <span class=\"pre\">:setting:`DEBUG`</span> <span class=\"pre\">为</span> <span class=\"pre\">``False</span></code>。</p>\n</section>\n</section>","rootId":"module-django.views","toc":[{"title":"Serving files in development","anchor":"serving-files-in-development","children":[]},{"title":"错误的视图","anchor":"error-views","children":[{"title":"404 (page not found) 视图","anchor":"the-404-page-not-found-view","children":[]},{"title":"The 500 (server error) view","anchor":"the-500-server-error-view","children":[]},{"title":"403 (HTTP Forbidden) 视图","anchor":"the-403-http-forbidden-view","children":[]},{"title":"400 (bad request) 视图","anchor":"the-400-bad-request-view","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API参考","url":"/zh-hans/3.0/ref/"}],"prev":{"docname":"ref/validators","title":"Validators","url":"/zh-hans/3.0/ref/validators/"},"next":{"docname":"misc/index","title":"元文件和杂记","url":"/zh-hans/3.0/misc/"},"formats":{"html":"/zh-hans/3.0/ref/views/","markdown":"/zh-hans/3.0/ref/views.md","json":"/zh-hans/3.0/ref/views.json"},"source":"https://github.com/django/django/blob/stable/3.0.x/docs/ref/views.txt","official":"https://docs.djangoproject.com/zh-hans/3.0/ref/views/","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"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}