<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Django shortcut functions — Django 1.4.21 documentation</title> <link rel="stylesheet" href="../../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../', VERSION: '1.4.21', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../../_static/jquery.js"></script> <script type="text/javascript" src="../../_static/underscore.js"></script> <script type="text/javascript" src="../../_static/doctools.js"></script> <link rel="top" title="Django 1.4.21 documentation" href="../../index.html" /> <link rel="up" title="Handling HTTP requests" href="index.html" /> <link rel="next" title="Generic views" href="generic-views.html" /> <link rel="prev" title="File Uploads" href="file-uploads.html" /> <script type="text/javascript" src="../../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../../ref/templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../index.html">Django 1.4.21 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../../index.html">Home</a> | <a title="Table of contents" href="../../contents.html">Table of contents</a> | <a title="Global index" href="../../genindex.html">Index</a> | <a title="Module index" href="../../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="file-uploads.html" title="File Uploads">previous</a> | <a href="../index.html" title="Using Django" accesskey="U">up</a> | <a href="generic-views.html" title="Generic views">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="topics-http-shortcuts"> <div class="section" id="s-module-django.shortcuts"> <span id="s-django-shortcut-functions"></span><span id="module-django.shortcuts"></span><span id="django-shortcut-functions"></span><h1>Django shortcut functions<a class="headerlink" href="#module-django.shortcuts" title="Permalink to this headline">¶</a></h1> <p id="index-0">The package <tt class="docutils literal"><span class="pre">django.shortcuts</span></tt> collects helper functions and classes that “span” multiple levels of MVC. In other words, these functions/classes introduce controlled coupling for convenience’s sake.</p> <div class="section" id="s-render"> <span id="render"></span><h2><tt class="docutils literal"><span class="pre">render</span></tt><a class="headerlink" href="#render" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="django.shortcuts.render"> <tt class="descname">render</tt>(<em>request, template_name[, dictionary][, context_instance][, content_type][, status][, current_app]</em>)<a class="headerlink" href="#django.shortcuts.render" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.3:</span> <a class="reference internal" href="../../releases/1.3.html"><em>Please see the release notes</em></a></div> <p>Combines a given template with a given context dictionary and returns an <a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></tt></a> object with that rendered text.</p> <p><a class="reference internal" href="../../ref/templates/api.html#render" title="render"><tt class="xref py py-func docutils literal"><span class="pre">render()</span></tt></a> is the same as a call to <a class="reference internal" href="#django.shortcuts.render_to_response" title="django.shortcuts.render_to_response"><tt class="xref py py-func docutils literal"><span class="pre">render_to_response()</span></tt></a> with a <cite>context_instance</cite> argument that forces the use of a <a class="reference internal" href="../../ref/templates/api.html#django.template.RequestContext" title="django.template.RequestContext"><tt class="xref py py-class docutils literal"><span class="pre">RequestContext</span></tt></a>.</p> </dd></dl> <div class="section" id="s-required-arguments"> <span id="required-arguments"></span><h3>Required arguments<a class="headerlink" href="#required-arguments" title="Permalink to this headline">¶</a></h3> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">request</span></tt></dt> <dd>The request object used to generate this response.</dd> <dt><tt class="docutils literal"><span class="pre">template_name</span></tt></dt> <dd>The full name of a template to use or sequence of template names.</dd> </dl> </div> <div class="section" id="s-optional-arguments"> <span id="optional-arguments"></span><h3>Optional arguments<a class="headerlink" href="#optional-arguments" title="Permalink to this headline">¶</a></h3> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">dictionary</span></tt></dt> <dd>A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the view will call it just before rendering the template.</dd> <dt><tt class="docutils literal"><span class="pre">context_instance</span></tt></dt> <dd>The context instance to render the template with. By default, the template will be rendered with a <tt class="docutils literal"><span class="pre">RequestContext</span></tt> instance (filled with values from <tt class="docutils literal"><span class="pre">request</span></tt> and <tt class="docutils literal"><span class="pre">dictionary</span></tt>).</dd> <dt><tt class="docutils literal"><span class="pre">content_type</span></tt></dt> <dd>The MIME type to use for the resulting document. Defaults to the value of the <a class="reference internal" href="../../ref/settings.html#std:setting-DEFAULT_CONTENT_TYPE"><tt class="xref std std-setting docutils literal"><span class="pre">DEFAULT_CONTENT_TYPE</span></tt></a> setting.</dd> <dt><tt class="docutils literal"><span class="pre">status</span></tt></dt> <dd>The status code for the response. Defaults to <tt class="docutils literal"><span class="pre">200</span></tt>.</dd> <dt><tt class="docutils literal"><span class="pre">current_app</span></tt></dt> <dd>A hint indicating which application contains the current view. See the <a class="reference internal" href="urls.html#topics-http-reversing-url-namespaces"><em>namespaced URL resolution strategy</em></a> for more information.</dd> </dl> </div> <div class="section" id="s-example"> <span id="example"></span><h3>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h3> <p>The following example renders the template <tt class="docutils literal"><span class="pre">myapp/index.html</span></tt> with the MIME type <em class="mimetype">application/xhtml+xml</em>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render</span> <span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="c"># View code here...</span> <span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s">'myapp/index.html'</span><span class="p">,</span> <span class="p">{</span><span class="s">"foo"</span><span class="p">:</span> <span class="s">"bar"</span><span class="p">},</span> <span class="n">content_type</span><span class="o">=</span><span class="s">"application/xhtml+xml"</span><span class="p">)</span> </pre></div> </div> <p>This example is equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span> <span class="kn">from</span> <span class="nn">django.template</span> <span class="kn">import</span> <span class="n">RequestContext</span><span class="p">,</span> <span class="n">loader</span> <span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="c"># View code here...</span> <span class="n">t</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="s">'myapp/template.html'</span><span class="p">)</span> <span class="n">c</span> <span class="o">=</span> <span class="n">RequestContext</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="p">{</span><span class="s">'foo'</span><span class="p">:</span> <span class="s">'bar'</span><span class="p">})</span> <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">c</span><span class="p">),</span> <span class="n">content_type</span><span class="o">=</span><span class="s">"application/xhtml+xml"</span><span class="p">)</span> </pre></div> </div> </div> </div> <div class="section" id="s-render-to-response"> <span id="render-to-response"></span><h2><tt class="docutils literal"><span class="pre">render_to_response</span></tt><a class="headerlink" href="#render-to-response" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="django.shortcuts.render_to_response"> <tt class="descname">render_to_response</tt>(<em>template_name[, dictionary][, context_instance][, mimetype]</em>)<a class="headerlink" href="#django.shortcuts.render_to_response" title="Permalink to this definition">¶</a></dt> <dd><p>Renders a given template with a given context dictionary and returns an <a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></tt></a> object with that rendered text.</p> </dd></dl> <div class="section" id="s-id1"> <span id="id1"></span><h3>Required arguments<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">template_name</span></tt></dt> <dd>The full name of a template to use or sequence of template names. If a sequence is given, the first template that exists will be used. See the <a class="reference internal" href="../../ref/templates/api.html#ref-templates-api-the-python-api"><em>template loader documentation</em></a> for more information on how templates are found.</dd> </dl> </div> <div class="section" id="s-id2"> <span id="id2"></span><h3>Optional arguments<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">dictionary</span></tt></dt> <dd>A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the view will call it just before rendering the template.</dd> <dt><tt class="docutils literal"><span class="pre">context_instance</span></tt></dt> <dd><p class="first">The context instance to render the template with. By default, the template will be rendered with a <a class="reference internal" href="../../ref/templates/api.html#django.template.Context" title="django.template.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> instance (filled with values from <tt class="docutils literal"><span class="pre">dictionary</span></tt>). If you need to use <a class="reference internal" href="../../ref/templates/api.html#subclassing-context-requestcontext"><em>context processors</em></a>, render the template with a <a class="reference internal" href="../../ref/templates/api.html#django.template.RequestContext" title="django.template.RequestContext"><tt class="xref py py-class docutils literal"><span class="pre">RequestContext</span></tt></a> instance instead. Your code might look something like this:</p> <div class="last highlight-python"><div class="highlight"><pre><span class="k">return</span> <span class="n">render_to_response</span><span class="p">(</span><span class="s">'my_template.html'</span><span class="p">,</span> <span class="n">my_data_dictionary</span><span class="p">,</span> <span class="n">context_instance</span><span class="o">=</span><span class="n">RequestContext</span><span class="p">(</span><span class="n">request</span><span class="p">))</span> </pre></div> </div> </dd> <dt><tt class="docutils literal"><span class="pre">mimetype</span></tt></dt> <dd>The MIME type to use for the resulting document. Defaults to the value of the <a class="reference internal" href="../../ref/settings.html#std:setting-DEFAULT_CONTENT_TYPE"><tt class="xref std std-setting docutils literal"><span class="pre">DEFAULT_CONTENT_TYPE</span></tt></a> setting.</dd> </dl> </div> <div class="section" id="s-id3"> <span id="id3"></span><h3>Example<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3> <p>The following example renders the template <tt class="docutils literal"><span class="pre">myapp/index.html</span></tt> with the MIME type <em class="mimetype">application/xhtml+xml</em>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render_to_response</span> <span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="c"># View code here...</span> <span class="k">return</span> <span class="n">render_to_response</span><span class="p">(</span><span class="s">'myapp/index.html'</span><span class="p">,</span> <span class="p">{</span><span class="s">"foo"</span><span class="p">:</span> <span class="s">"bar"</span><span class="p">},</span> <span class="n">mimetype</span><span class="o">=</span><span class="s">"application/xhtml+xml"</span><span class="p">)</span> </pre></div> </div> <p>This example is equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span> <span class="kn">from</span> <span class="nn">django.template</span> <span class="kn">import</span> <span class="n">Context</span><span class="p">,</span> <span class="n">loader</span> <span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="c"># View code here...</span> <span class="n">t</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="s">'myapp/template.html'</span><span class="p">)</span> <span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">({</span><span class="s">'foo'</span><span class="p">:</span> <span class="s">'bar'</span><span class="p">})</span> <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">c</span><span class="p">),</span> <span class="n">mimetype</span><span class="o">=</span><span class="s">"application/xhtml+xml"</span><span class="p">)</span> </pre></div> </div> </div> </div> <div class="section" id="s-redirect"> <span id="redirect"></span><h2><tt class="docutils literal"><span class="pre">redirect</span></tt><a class="headerlink" href="#redirect" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="django.shortcuts.redirect"> <tt class="descname">redirect</tt>(<em>to</em><span class="optional">[</span>, <em>permanent=False</em><span class="optional">]</span>, <em>*args</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.shortcuts.redirect" title="Permalink to this definition">¶</a></dt> <dd><p>Returns an <a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponseRedirect" title="django.http.HttpResponseRedirect"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponseRedirect</span></tt></a> to the appropriate URL for the arguments passed.</p> <p>The arguments could be:</p> <ul class="simple"> <li>A model: the model’s <cite>get_absolute_url()</cite> function will be called.</li> <li>A view name, possibly with arguments: <cite>urlresolvers.reverse()</cite> will be used to reverse-resolve the name.</li> <li>A URL, which will be used as-is for the redirect location.</li> </ul> <p>By default issues a temporary redirect; pass <tt class="docutils literal"><span class="pre">permanent=True</span></tt> to issue a permanent redirect</p> </dd></dl> <div class="section" id="s-examples"> <span id="examples"></span><h3>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h3> <p>You can use the <a class="reference internal" href="#django.shortcuts.redirect" title="django.shortcuts.redirect"><tt class="xref py py-func docutils literal"><span class="pre">redirect()</span></tt></a> function in a number of ways.</p> <ol class="arabic"> <li><p class="first">By passing some object; that object’s <a class="reference internal" href="../../ref/models/instances.html#django.db.models.Model.get_absolute_url" title="django.db.models.Model.get_absolute_url"><tt class="xref py py-meth docutils literal"><span class="pre">get_absolute_url()</span></tt></a> method will be called to figure out the redirect URL:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="o">...</span> <span class="nb">object</span> <span class="o">=</span> <span class="n">MyModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o">...</span><span class="p">)</span> <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="nb">object</span><span class="p">)</span> </pre></div> </div> </li> <li><p class="first">By passing the name of a view and optionally some positional or keyword arguments; the URL will be reverse resolved using the <a class="reference internal" href="urls.html#django.core.urlresolvers.reverse" title="django.core.urlresolvers.reverse"><tt class="xref py py-func docutils literal"><span class="pre">reverse()</span></tt></a> method:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="o">...</span> <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="s">'some-view-name'</span><span class="p">,</span> <span class="n">foo</span><span class="o">=</span><span class="s">'bar'</span><span class="p">)</span> </pre></div> </div> </li> <li><p class="first">By passing a hardcoded URL to redirect to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="o">...</span> <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="s">'/some/url/'</span><span class="p">)</span> </pre></div> </div> <p>This also works with full URLs:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="o">...</span> <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="s">'http://example.com/'</span><span class="p">)</span> </pre></div> </div> </li> </ol> <p>By default, <a class="reference internal" href="#django.shortcuts.redirect" title="django.shortcuts.redirect"><tt class="xref py py-func docutils literal"><span class="pre">redirect()</span></tt></a> returns a temporary redirect. All of the above forms accept a <tt class="docutils literal"><span class="pre">permanent</span></tt> argument; if set to <tt class="docutils literal"><span class="pre">True</span></tt> a permanent redirect will be returned:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="o">...</span> <span class="nb">object</span> <span class="o">=</span> <span class="n">MyModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o">...</span><span class="p">)</span> <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="nb">object</span><span class="p">,</span> <span class="n">permanent</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> </pre></div> </div> </div> </div> <div class="section" id="s-get-object-or-404"> <span id="get-object-or-404"></span><h2><tt class="docutils literal"><span class="pre">get_object_or_404</span></tt><a class="headerlink" href="#get-object-or-404" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="django.shortcuts.get_object_or_404"> <tt class="descname">get_object_or_404</tt>(<em>klass</em>, <em>*args</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.shortcuts.get_object_or_404" title="Permalink to this definition">¶</a></dt> <dd><p>Calls <a class="reference internal" href="../../ref/models/querysets.html#django.db.models.query.QuerySet.get" title="django.db.models.query.QuerySet.get"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></a> on a given model manager, but it raises <a class="reference internal" href="views.html#django.http.Http404" title="django.http.Http404"><tt class="xref py py-class docutils literal"><span class="pre">Http404</span></tt></a> instead of the model’s <a class="reference internal" href="../../ref/exceptions.html#django.core.exceptions.DoesNotExist" title="django.core.exceptions.DoesNotExist"><tt class="xref py py-class docutils literal"><span class="pre">DoesNotExist</span></tt></a> exception.</p> </dd></dl> <div class="section" id="s-id4"> <span id="id4"></span><h3>Required arguments<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h3> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">klass</span></tt></dt> <dd>A <a class="reference internal" href="../../ref/models/instances.html#django.db.models.Model" title="django.db.models.Model"><tt class="xref py py-class docutils literal"><span class="pre">Model</span></tt></a>, <a class="reference internal" href="../db/managers.html#django.db.models.Manager" title="django.db.models.Manager"><tt class="xref py py-class docutils literal"><span class="pre">Manager</span></tt></a> or <a class="reference internal" href="../../ref/models/querysets.html#django.db.models.query.QuerySet" title="django.db.models.query.QuerySet"><tt class="xref py py-class docutils literal"><span class="pre">QuerySet</span></tt></a> instance from which to get the object.</dd> <dt><tt class="docutils literal"><span class="pre">**kwargs</span></tt></dt> <dd>Lookup parameters, which should be in the format accepted by <tt class="docutils literal"><span class="pre">get()</span></tt> and <tt class="docutils literal"><span class="pre">filter()</span></tt>.</dd> </dl> </div> <div class="section" id="s-id5"> <span id="id5"></span><h3>Example<a class="headerlink" href="#id5" title="Permalink to this headline">¶</a></h3> <p>The following example gets the object with the primary key of 1 from <tt class="docutils literal"><span class="pre">MyModel</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">get_object_or_404</span> <span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="n">my_object</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">MyModel</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> </pre></div> </div> <p>This example is equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">Http404</span> <span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="k">try</span><span class="p">:</span> <span class="n">my_object</span> <span class="o">=</span> <span class="n">MyModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="k">except</span> <span class="n">MyModel</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">:</span> <span class="k">raise</span> <span class="n">Http404</span> </pre></div> </div> <p>Note: As with <tt class="docutils literal"><span class="pre">get()</span></tt>, a <a class="reference internal" href="../../ref/exceptions.html#django.core.exceptions.MultipleObjectsReturned" title="django.core.exceptions.MultipleObjectsReturned"><tt class="xref py py-class docutils literal"><span class="pre">MultipleObjectsReturned</span></tt></a> exception will be raised if more than one object is found.</p> </div> </div> <div class="section" id="s-get-list-or-404"> <span id="get-list-or-404"></span><h2><tt class="docutils literal"><span class="pre">get_list_or_404</span></tt><a class="headerlink" href="#get-list-or-404" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="django.shortcuts.get_list_or_404"> <tt class="descname">get_list_or_404</tt>(<em>klass</em>, <em>*args</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.shortcuts.get_list_or_404" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the result of <a class="reference internal" href="../../ref/models/querysets.html#django.db.models.query.QuerySet.filter" title="django.db.models.query.QuerySet.filter"><tt class="xref py py-meth docutils literal"><span class="pre">filter()</span></tt></a> on a given model manager, raising <a class="reference internal" href="views.html#django.http.Http404" title="django.http.Http404"><tt class="xref py py-class docutils literal"><span class="pre">Http404</span></tt></a> if the resulting list is empty.</p> </dd></dl> <div class="section" id="s-id6"> <span id="id6"></span><h3>Required arguments<a class="headerlink" href="#id6" title="Permalink to this headline">¶</a></h3> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">klass</span></tt></dt> <dd>A <a class="reference internal" href="../../ref/models/instances.html#django.db.models.Model" title="django.db.models.Model"><tt class="xref py py-class docutils literal"><span class="pre">Model</span></tt></a>, <a class="reference internal" href="../db/managers.html#django.db.models.Manager" title="django.db.models.Manager"><tt class="xref py py-class docutils literal"><span class="pre">Manager</span></tt></a> or <a class="reference internal" href="../../ref/models/querysets.html#django.db.models.query.QuerySet" title="django.db.models.query.QuerySet"><tt class="xref py py-class docutils literal"><span class="pre">QuerySet</span></tt></a> instance from which to get the list.</dd> <dt><tt class="docutils literal"><span class="pre">**kwargs</span></tt></dt> <dd>Lookup parameters, which should be in the format accepted by <tt class="docutils literal"><span class="pre">get()</span></tt> and <tt class="docutils literal"><span class="pre">filter()</span></tt>.</dd> </dl> </div> <div class="section" id="s-id7"> <span id="id7"></span><h3>Example<a class="headerlink" href="#id7" title="Permalink to this headline">¶</a></h3> <p>The following example gets all published objects from <tt class="docutils literal"><span class="pre">MyModel</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">get_list_or_404</span> <span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="n">my_objects</span> <span class="o">=</span> <span class="n">get_list_or_404</span><span class="p">(</span><span class="n">MyModel</span><span class="p">,</span> <span class="n">published</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> </pre></div> </div> <p>This example is equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">Http404</span> <span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="n">my_objects</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">MyModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">published</span><span class="o">=</span><span class="bp">True</span><span class="p">))</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">my_objects</span><span class="p">:</span> <span class="k">raise</span> <span class="n">Http404</span> </pre></div> </div> </div> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Django shortcut functions</a><ul> <li><a class="reference internal" href="#render"><tt class="docutils literal"><span class="pre">render</span></tt></a><ul> <li><a class="reference internal" href="#required-arguments">Required arguments</a></li> <li><a class="reference internal" href="#optional-arguments">Optional arguments</a></li> <li><a class="reference internal" href="#example">Example</a></li> </ul> </li> <li><a class="reference internal" href="#render-to-response"><tt class="docutils literal"><span class="pre">render_to_response</span></tt></a><ul> <li><a class="reference internal" href="#id1">Required arguments</a></li> <li><a class="reference internal" href="#id2">Optional arguments</a></li> <li><a class="reference internal" href="#id3">Example</a></li> </ul> </li> <li><a class="reference internal" href="#redirect"><tt class="docutils literal"><span class="pre">redirect</span></tt></a><ul> <li><a class="reference internal" href="#examples">Examples</a></li> </ul> </li> <li><a class="reference internal" href="#get-object-or-404"><tt class="docutils literal"><span class="pre">get_object_or_404</span></tt></a><ul> <li><a class="reference internal" href="#id4">Required arguments</a></li> <li><a class="reference internal" href="#id5">Example</a></li> </ul> </li> <li><a class="reference internal" href="#get-list-or-404"><tt class="docutils literal"><span class="pre">get_list_or_404</span></tt></a><ul> <li><a class="reference internal" href="#id6">Required arguments</a></li> <li><a class="reference internal" href="#id7">Example</a></li> </ul> </li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="file-uploads.html">File Uploads</a></li> <li>Next: <a href="generic-views.html">Generic views</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../index.html">Django 1.4.21 documentation</a> <ul><li><a href="../index.html">Using Django</a> <ul><li><a href="index.html">Handling HTTP requests</a> <ul><li>Django shortcut functions</li></ul> </li></ul></li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/topics/http/shortcuts.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Jul 10, 2015</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="file-uploads.html" title="File Uploads">previous</a> | <a href="../index.html" title="Using Django" accesskey="U">up</a> | <a href="generic-views.html" title="Generic views">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>