{"title":"Django shortcut functions","version":"3.0","locale":"ko","docname":"topics/http/shortcuts","url":"/ko/3.0/topics/http/shortcuts/","canonical":"https://djangodocs.dev/ko/3.0/topics/http/shortcuts/","summary":"The package django.shortcuts collects helper functions and classes that “span” multiple levels of MVC.","html":"<span id=\"django-shortcut-functions\"></span><h1>Django shortcut functions<a class=\"heading-anchor\" href=\"#module-django.shortcuts\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p id=\"index-0\">The package <code class=\"docutils literal notranslate\">django.shortcuts</code> collects helper functions and classes that\n“span” multiple levels of MVC. In other words, these functions/classes\nintroduce controlled coupling for convenience’s sake.</p>\n<section id=\"render\">\n<h2><code class=\"docutils literal notranslate\">render()</code><a class=\"heading-anchor\" href=\"#render\"><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.shortcuts.render\">\n<span class=\"sig-name descname\">render</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em>, <em class=\"sig-param\">template_name</em>, <em class=\"sig-param\">context<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">content_type<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">status<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">using<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.shortcuts.render\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Combines a given template with a given context dictionary and returns an\n<a class=\"reference internal\" href=\"/ko/3.0/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\">HttpResponse</code></a> object with that rendered text.</p>\n<p>Django does not provide a shortcut function which returns a\n<a class=\"reference internal\" href=\"/ko/3.0/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a> because the constructor\nof <a class=\"reference internal\" href=\"/ko/3.0/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a> offers the same level\nof convenience as <a class=\"reference internal\" href=\"#django.shortcuts.render\" title=\"django.shortcuts.render\"><code class=\"xref py py-func docutils literal notranslate\">render()</code></a>.</p>\n</dd></dl>\n\n<section id=\"required-arguments\">\n<h3>Required arguments<a class=\"heading-anchor\" href=\"#required-arguments\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"simple\">\n<dt><code class=\"docutils literal notranslate\">request</code></dt><dd><p>The request object used to generate this response.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">template_name</code></dt><dd><p>The full name of a template to use or sequence of template names. If a\nsequence is given, the first template that exists will be used. See the\n<a class=\"reference internal\" href=\"/ko/3.0/topics/templates/#template-loading\"><span class=\"std std-ref\">template loading documentation</span></a> for more\ninformation on how templates are found.</p>\n</dd>\n</dl>\n</section>\n<section id=\"optional-arguments\">\n<h3>Optional arguments<a class=\"heading-anchor\" href=\"#optional-arguments\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"simple\">\n<dt><code class=\"docutils literal notranslate\">context</code></dt><dd><p>A dictionary of values to add to the template context. By default, this\nis an empty dictionary. If a value in the dictionary is callable, the\nview will call it just before rendering the template.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">content_type</code></dt><dd><p>The MIME type to use for the resulting document. Defaults to\n<code class=\"docutils literal notranslate\">'text/html'</code>.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">status</code></dt><dd><p>The status code for the response. Defaults to <code class=\"docutils literal notranslate\">200</code>.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">using</code></dt><dd><p>The <a class=\"reference internal\" href=\"/ko/3.0/ref/settings/#std-setting-TEMPLATES-NAME\"><code class=\"xref std std-setting docutils literal notranslate\">NAME</code></a> of a template engine to use for\nloading the template.</p>\n</dd>\n</dl>\n</section>\n<section id=\"example\">\n<h3>Example<a class=\"heading-anchor\" href=\"#example\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The following example renders the template <code class=\"docutils literal notranslate\">myapp/index.html</code> with the\nMIME type <em class=\"mimetype\">application/xhtml+xml</em>:</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.shortcuts</span> <span class=\"kn\">import</span> render\n\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"c1\"># View code here...</span>\n    <span class=\"k\">return</span> render<span class=\"p\">(</span>request<span class=\"p\">,</span> <span class=\"s1\">&#39;myapp/index.html&#39;</span><span class=\"p\">,</span> <span class=\"p\">{</span>\n        <span class=\"s1\">&#39;foo&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;bar&#39;</span><span class=\"p\">,</span>\n    <span class=\"p\">},</span> content_type<span class=\"o\">=</span><span class=\"s1\">&#39;application/xhtml+xml&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>This example is equivalent to:</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.http</span> <span class=\"kn\">import</span> HttpResponse\n<span class=\"kn\">from</span> <span class=\"nn\">django.template</span> <span class=\"kn\">import</span> loader\n\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"c1\"># View code here...</span>\n    t <span class=\"o\">=</span> loader<span class=\"o\">.</span>get_template<span class=\"p\">(</span><span class=\"s1\">&#39;myapp/index.html&#39;</span><span class=\"p\">)</span>\n    c <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"s1\">&#39;foo&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;bar&#39;</span><span class=\"p\">}</span>\n    <span class=\"k\">return</span> HttpResponse<span class=\"p\">(</span>t<span class=\"o\">.</span>render<span class=\"p\">(</span>c<span class=\"p\">,</span> request<span class=\"p\">),</span> content_type<span class=\"o\">=</span><span class=\"s1\">&#39;application/xhtml+xml&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"redirect\">\n<h2><code class=\"docutils literal notranslate\">redirect()</code><a class=\"heading-anchor\" href=\"#redirect\"><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.shortcuts.redirect\">\n<span class=\"sig-name descname\">redirect</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">to</em>, <em class=\"sig-param\"><span class=\"o\">*</span>args</em>, <em class=\"sig-param\">permanent<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.shortcuts.redirect\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns an <a class=\"reference internal\" href=\"/ko/3.0/ref/request-response/#django.http.HttpResponseRedirect\" title=\"django.http.HttpResponseRedirect\"><code class=\"xref py py-class docutils literal notranslate\">HttpResponseRedirect</code></a> to the appropriate URL\nfor the arguments passed.</p>\n<p>The arguments could be:</p>\n<ul class=\"simple\">\n<li><p>A model: the model’s <a class=\"reference internal\" href=\"/ko/3.0/ref/models/instances/#django.db.models.Model.get_absolute_url\" title=\"django.db.models.Model.get_absolute_url\"><code class=\"xref py py-meth docutils literal notranslate\">get_absolute_url()</code></a>\nfunction will be called.</p></li>\n<li><p>A view name, possibly with arguments: <a class=\"reference internal\" href=\"/ko/3.0/ref/urlresolvers/#django.urls.reverse\" title=\"django.urls.reverse\"><code class=\"xref py py-func docutils literal notranslate\">reverse()</code></a> will be\nused to reverse-resolve the name.</p></li>\n<li><p>An absolute or relative URL, which will be used as-is for the redirect\nlocation.</p></li>\n</ul>\n<p>By default issues a temporary redirect; pass <code class=\"docutils literal notranslate\">permanent=True</code> to issue a\npermanent redirect.</p>\n</dd></dl>\n\n<section id=\"examples\">\n<h3>예제<a class=\"heading-anchor\" href=\"#examples\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>You can use the <a class=\"reference internal\" href=\"#django.shortcuts.redirect\" title=\"django.shortcuts.redirect\"><code class=\"xref py py-func docutils literal notranslate\">redirect()</code></a> function in a number of ways.</p>\n<ol class=\"arabic\">\n<li><p>By passing some object; that object’s\n<a class=\"reference internal\" href=\"/ko/3.0/ref/models/instances/#django.db.models.Model.get_absolute_url\" title=\"django.db.models.Model.get_absolute_url\"><code class=\"xref py py-meth docutils literal notranslate\">get_absolute_url()</code></a> method will be called\nto figure out the redirect 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\">from</span> <span class=\"nn\">django.shortcuts</span> <span class=\"kn\">import</span> redirect\n\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"o\">...</span>\n    obj <span class=\"o\">=</span> MyModel<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">)</span>\n    <span class=\"k\">return</span> redirect<span class=\"p\">(</span>obj<span class=\"p\">)</span>\n</code></pre></div>\n</li>\n<li><p>By passing the name of a view and optionally some positional or\nkeyword arguments; the URL will be reverse resolved using the\n<a class=\"reference internal\" href=\"/ko/3.0/ref/urlresolvers/#django.urls.reverse\" title=\"django.urls.reverse\"><code class=\"xref py py-func docutils literal notranslate\">reverse()</code></a> method:</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=\"o\">...</span>\n    <span class=\"k\">return</span> redirect<span class=\"p\">(</span><span class=\"s1\">&#39;some-view-name&#39;</span><span class=\"p\">,</span> foo<span class=\"o\">=</span><span class=\"s1\">&#39;bar&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n</li>\n<li><p>By passing a hardcoded URL to redirect to:</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=\"o\">...</span>\n    <span class=\"k\">return</span> redirect<span class=\"p\">(</span><span class=\"s1\">&#39;/some/url/&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>This also works with full URLs:</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=\"o\">...</span>\n    <span class=\"k\">return</span> redirect<span class=\"p\">(</span><span class=\"s1\">&#39;https://example.com/&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n</li>\n</ol>\n<p>By default, <a class=\"reference internal\" href=\"#django.shortcuts.redirect\" title=\"django.shortcuts.redirect\"><code class=\"xref py py-func docutils literal notranslate\">redirect()</code></a> returns a temporary redirect. All of the above\nforms accept a <code class=\"docutils literal notranslate\">permanent</code> argument; if set to <code class=\"docutils literal notranslate\">True</code> a permanent redirect\nwill be returned:</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=\"o\">...</span>\n    obj <span class=\"o\">=</span> MyModel<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">)</span>\n    <span class=\"k\">return</span> redirect<span class=\"p\">(</span>obj<span class=\"p\">,</span> permanent<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"get-object-or-404\">\n<h2><code class=\"docutils literal notranslate\">get_object_or_404()</code><a class=\"heading-anchor\" href=\"#get-object-or-404\"><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.shortcuts.get_object_or_404\">\n<span class=\"sig-name descname\">get_object_or_404</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">klass</em>, <em class=\"sig-param\"><span class=\"o\">*</span>args</em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.shortcuts.get_object_or_404\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Calls <a class=\"reference internal\" href=\"/ko/3.0/ref/models/querysets/#django.db.models.query.QuerySet.get\" title=\"django.db.models.query.QuerySet.get\"><code class=\"xref py py-meth docutils literal notranslate\">get()</code></a> on a given model manager,\nbut it raises <a class=\"reference internal\" href=\"/ko/3.0/topics/http/views/#django.http.Http404\" title=\"django.http.Http404\"><code class=\"xref py py-class docutils literal notranslate\">Http404</code></a> instead of the model’s\n<a class=\"reference internal\" href=\"/ko/3.0/ref/models/class/#django.db.models.Model.DoesNotExist\" title=\"django.db.models.Model.DoesNotExist\"><code class=\"xref py py-class docutils literal notranslate\">DoesNotExist</code></a> exception.</p>\n</dd></dl>\n\n<section id=\"id1\">\n<h3>Required arguments<a class=\"heading-anchor\" href=\"#id1\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"simple\">\n<dt><code class=\"docutils literal notranslate\">klass</code></dt><dd><p>A <a class=\"reference internal\" href=\"/ko/3.0/ref/models/instances/#django.db.models.Model\" title=\"django.db.models.Model\"><code class=\"xref py py-class docutils literal notranslate\">Model</code></a> class,\na <a class=\"reference internal\" href=\"/ko/3.0/topics/db/managers/#django.db.models.Manager\" title=\"django.db.models.Manager\"><code class=\"xref py py-class docutils literal notranslate\">Manager</code></a>,\nor a <a class=\"reference internal\" href=\"/ko/3.0/ref/models/querysets/#django.db.models.query.QuerySet\" title=\"django.db.models.query.QuerySet\"><code class=\"xref py py-class docutils literal notranslate\">QuerySet</code></a> instance from which to get\nthe object.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">**kwargs</code></dt><dd><p>Lookup parameters, which should be in the format accepted by <code class=\"docutils literal notranslate\">get()</code> and\n<code class=\"docutils literal notranslate\">filter()</code>.</p>\n</dd>\n</dl>\n</section>\n<section id=\"id2\">\n<h3>Example<a class=\"heading-anchor\" href=\"#id2\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The following example gets the object with the primary key of 1 from\n<code class=\"docutils literal notranslate\">MyModel</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.shortcuts</span> <span class=\"kn\">import</span> get_object_or_404\n\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    obj <span class=\"o\">=</span> get_object_or_404<span class=\"p\">(</span>MyModel<span class=\"p\">,</span> pk<span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>This example is equivalent to:</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.http</span> <span class=\"kn\">import</span> Http404\n\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"k\">try</span><span class=\"p\">:</span>\n        obj <span class=\"o\">=</span> MyModel<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>pk<span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n    <span class=\"k\">except</span> MyModel<span class=\"o\">.</span>DoesNotExist<span class=\"p\">:</span>\n        <span class=\"k\">raise</span> Http404<span class=\"p\">(</span><span class=\"s2\">&quot;No MyModel matches the given query.&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>The most common use case is to pass a <a class=\"reference internal\" href=\"/ko/3.0/ref/models/instances/#django.db.models.Model\" title=\"django.db.models.Model\"><code class=\"xref py py-class docutils literal notranslate\">Model</code></a>, as\nshown above. However, you can also pass a\n<a class=\"reference internal\" href=\"/ko/3.0/ref/models/querysets/#django.db.models.query.QuerySet\" title=\"django.db.models.query.QuerySet\"><code class=\"xref py py-class docutils literal notranslate\">QuerySet</code></a> 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>queryset <span class=\"o\">=</span> Book<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>title__startswith<span class=\"o\">=</span><span class=\"s1\">&#39;M&#39;</span><span class=\"p\">)</span>\nget_object_or_404<span class=\"p\">(</span>queryset<span class=\"p\">,</span> pk<span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>The above example is a bit contrived since it’s equivalent to doing:</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>get_object_or_404<span class=\"p\">(</span>Book<span class=\"p\">,</span> title__startswith<span class=\"o\">=</span><span class=\"s1\">&#39;M&#39;</span><span class=\"p\">,</span> pk<span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>but it can be useful if you are passed the <code class=\"docutils literal notranslate\">queryset</code> variable from somewhere\nelse.</p>\n<p>Finally, you can also use a <a class=\"reference internal\" href=\"/ko/3.0/topics/db/managers/#django.db.models.Manager\" title=\"django.db.models.Manager\"><code class=\"xref py py-class docutils literal notranslate\">Manager</code></a>. This is useful\nfor example if you have a\n<a class=\"reference internal\" href=\"/ko/3.0/topics/db/managers/#custom-managers\"><span class=\"std std-ref\">custom manager</span></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>get_object_or_404<span class=\"p\">(</span>Book<span class=\"o\">.</span>dahl_objects<span class=\"p\">,</span> title<span class=\"o\">=</span><span class=\"s1\">&#39;Matilda&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>You can also use\n<a class=\"reference internal\" href=\"/ko/3.0/ref/models/relations/#django.db.models.fields.related.RelatedManager\" title=\"django.db.models.fields.related.RelatedManager\"><code class=\"xref py py-class docutils literal notranslate\">related managers</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>author <span class=\"o\">=</span> Author<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>name<span class=\"o\">=</span><span class=\"s1\">&#39;Roald Dahl&#39;</span><span class=\"p\">)</span>\nget_object_or_404<span class=\"p\">(</span>author<span class=\"o\">.</span>book_set<span class=\"p\">,</span> title<span class=\"o\">=</span><span class=\"s1\">&#39;Matilda&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Note: As with <code class=\"docutils literal notranslate\">get()</code>, a\n<a class=\"reference internal\" href=\"/ko/3.0/ref/exceptions/#django.core.exceptions.MultipleObjectsReturned\" title=\"django.core.exceptions.MultipleObjectsReturned\"><code class=\"xref py py-class docutils literal notranslate\">MultipleObjectsReturned</code></a> exception\nwill be raised if more than one object is found.</p>\n</section>\n</section>\n<section id=\"get-list-or-404\">\n<h2><code class=\"docutils literal notranslate\">get_list_or_404()</code><a class=\"heading-anchor\" href=\"#get-list-or-404\"><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.shortcuts.get_list_or_404\">\n<span class=\"sig-name descname\">get_list_or_404</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">klass</em>, <em class=\"sig-param\"><span class=\"o\">*</span>args</em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.shortcuts.get_list_or_404\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the result of <a class=\"reference internal\" href=\"/ko/3.0/ref/models/querysets/#django.db.models.query.QuerySet.filter\" title=\"django.db.models.query.QuerySet.filter\"><code class=\"xref py py-meth docutils literal notranslate\">filter()</code></a> on a\ngiven model manager cast to a list, raising <a class=\"reference internal\" href=\"/ko/3.0/topics/http/views/#django.http.Http404\" title=\"django.http.Http404\"><code class=\"xref py py-class docutils literal notranslate\">Http404</code></a> if\nthe resulting list is empty.</p>\n</dd></dl>\n\n<section id=\"id3\">\n<h3>Required arguments<a class=\"heading-anchor\" href=\"#id3\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"simple\">\n<dt><code class=\"docutils literal notranslate\">klass</code></dt><dd><p>A <a class=\"reference internal\" href=\"/ko/3.0/ref/models/instances/#django.db.models.Model\" title=\"django.db.models.Model\"><code class=\"xref py py-class docutils literal notranslate\">Model</code></a>, <a class=\"reference internal\" href=\"/ko/3.0/topics/db/managers/#django.db.models.Manager\" title=\"django.db.models.Manager\"><code class=\"xref py py-class docutils literal notranslate\">Manager</code></a> or\n<a class=\"reference internal\" href=\"/ko/3.0/ref/models/querysets/#django.db.models.query.QuerySet\" title=\"django.db.models.query.QuerySet\"><code class=\"xref py py-class docutils literal notranslate\">QuerySet</code></a> instance from which to get the\nlist.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">**kwargs</code></dt><dd><p>Lookup parameters, which should be in the format accepted by <code class=\"docutils literal notranslate\">get()</code> and\n<code class=\"docutils literal notranslate\">filter()</code>.</p>\n</dd>\n</dl>\n</section>\n<section id=\"id4\">\n<h3>Example<a class=\"heading-anchor\" href=\"#id4\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The following example gets all published objects from <code class=\"docutils literal notranslate\">MyModel</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.shortcuts</span> <span class=\"kn\">import</span> get_list_or_404\n\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    my_objects <span class=\"o\">=</span> get_list_or_404<span class=\"p\">(</span>MyModel<span class=\"p\">,</span> published<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>This example is equivalent to:</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.http</span> <span class=\"kn\">import</span> Http404\n\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    my_objects <span class=\"o\">=</span> <span class=\"nb\">list</span><span class=\"p\">(</span>MyModel<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>published<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">))</span>\n    <span class=\"k\">if</span> <span class=\"ow\">not</span> my_objects<span class=\"p\">:</span>\n        <span class=\"k\">raise</span> Http404<span class=\"p\">(</span><span class=\"s2\">&quot;No MyModel matches the given query.&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n</section>","rootId":"module-django.shortcuts","toc":[{"title":"render()","anchor":"render","children":[{"title":"Required arguments","anchor":"required-arguments","children":[]},{"title":"Optional arguments","anchor":"optional-arguments","children":[]},{"title":"Example","anchor":"example","children":[]}]},{"title":"redirect()","anchor":"redirect","children":[{"title":"예제","anchor":"examples","children":[]}]},{"title":"get_object_or_404()","anchor":"get-object-or-404","children":[{"title":"Required arguments","anchor":"id1","children":[]},{"title":"Example","anchor":"id2","children":[]}]},{"title":"get_list_or_404()","anchor":"get-list-or-404","children":[{"title":"Required arguments","anchor":"id3","children":[]},{"title":"Example","anchor":"id4","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Django 사용하기","url":"/ko/3.0/topics/"},{"docname":"topics/http/index","title":"Handling HTTP requests","url":"/ko/3.0/topics/http/"}],"prev":{"docname":"topics/http/file-uploads","title":"File Uploads","url":"/ko/3.0/topics/http/file-uploads/"},"next":{"docname":"topics/http/generic-views","title":"Generic views","url":"/ko/3.0/topics/http/generic-views/"},"formats":{"html":"/ko/3.0/topics/http/shortcuts/","markdown":"/ko/3.0/topics/http/shortcuts.md","json":"/ko/3.0/topics/http/shortcuts.json"},"source":"https://github.com/django/django/blob/stable/3.0.x/docs/topics/http/shortcuts.txt","official":"https://docs.djangoproject.com/ko/3.0/topics/http/shortcuts/","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"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}