{"title":"Writing your first Django app, part 3","version":"1.8","locale":"en","docname":"intro/tutorial03","url":"/en/1.8/intro/tutorial03/","canonical":"https://djangodocs.dev/en/1.8/intro/tutorial03/","summary":"This tutorial begins where Tutorial 2 left off. We’re continuing the Web-poll application and will focus on creating the public interface – “views.” Philosophy Link…","html":"<h1>Writing your first Django app, part 3<a class=\"heading-anchor\" href=\"#writing-your-first-django-app-part-3\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This tutorial begins where <a class=\"reference internal\" href=\"/en/1.8/intro/tutorial02/\"><span class=\"doc\">Tutorial 2</span></a> left off. We’re\ncontinuing the Web-poll application and will focus on creating the public\ninterface – “views.”</p>\n<section id=\"philosophy\">\n<h2>Philosophy<a class=\"heading-anchor\" href=\"#philosophy\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A view is a “type” of Web page in your Django application that generally serves\na specific function and has a specific template. For example, in a blog\napplication, you might have the following views:</p>\n<ul class=\"simple\">\n<li><p>Blog homepage – displays the latest few entries.</p></li>\n<li><p>Entry “detail” page – permalink page for a single entry.</p></li>\n<li><p>Year-based archive page – displays all months with entries in the\ngiven year.</p></li>\n<li><p>Month-based archive page – displays all days with entries in the\ngiven month.</p></li>\n<li><p>Day-based archive page – displays all entries in the given day.</p></li>\n<li><p>Comment action – handles posting comments to a given entry.</p></li>\n</ul>\n<p>In our poll application, we’ll have the following four views:</p>\n<ul class=\"simple\">\n<li><p>Question “index” page – displays the latest few questions.</p></li>\n<li><p>Question “detail” page – displays a question text, with no results but\nwith a form to vote.</p></li>\n<li><p>Question “results” page – displays results for a particular question.</p></li>\n<li><p>Vote action – handles voting for a particular choice in a particular\nquestion.</p></li>\n</ul>\n<p>In Django, web pages and other content are delivered by views. Each view is\nrepresented by a simple Python function (or method, in the case of class-based\nviews). Django will choose a view by examining the URL that’s requested (to be\nprecise, the part of the URL after the domain name).</p>\n<p>Now in your time on the web you may have come across such beauties as\n“ME2/Sites/dirmod.asp?sid=&amp;type=gen&amp;mod=Core+Pages&amp;gid=A6CD4967199A42D9B65B1B”.\nYou will be pleased to know that Django allows us much more elegant\n<em>URL patterns</em> than that.</p>\n<p>A URL pattern is simply the general form of a URL - for example:\n<code class=\"docutils literal notranslate\"><span class=\"pre\">/newsarchive/&lt;year&gt;/&lt;month&gt;/</span></code>.</p>\n<p>To get from a URL to a view, Django uses what are known as ‘URLconfs’. A\nURLconf maps URL patterns (described as regular expressions) to views.</p>\n<p>This tutorial provides basic instruction in the use of URLconfs, and you can\nrefer to <a class=\"reference internal\" href=\"/en/1.8/ref/urlresolvers/#module-django.core.urlresolvers\" title=\"django.core.urlresolvers\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.core.urlresolvers</span></code></a> for more information.</p>\n</section>\n<section id=\"write-your-first-view\">\n<h2>Write your first view<a class=\"heading-anchor\" href=\"#write-your-first-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Let’s write the first view. Open the file <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/views.py</span></code>\nand put the following Python code in it:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/views.py</code></figcaption><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=\"polls/views.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HttpResponse</span>\n\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">index</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"s2\">&quot;Hello, world. You&#39;re at the polls index.&quot;</span><span class=\"p\">)</span>\n</code></pre></figure>\n<p>This is the simplest view possible in Django. To call the view, we need to map\nit to a URL - and for this we need a URLconf.</p>\n<p>To create a URLconf in the polls directory, create a file called <code class=\"docutils literal notranslate\"><span class=\"pre\">urls.py</span></code>.\nYour app directory should now look like:</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=\"n\">polls</span><span class=\"o\">/</span>\n    <span class=\"fm\">__init__</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">admin</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">tests</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">urls</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">py</span>\n</code></pre></div>\n<p>In the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/urls.py</span></code> file include the following code:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/urls.py</code></figcaption><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=\"polls/urls.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">url</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^$&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">index</span><span class=\"p\">,</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;index&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>The next step is to point the root URLconf at the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls.urls</span></code> module. In\n<code class=\"docutils literal notranslate\"><span class=\"pre\">mysite/urls.py</span></code> insert an <a class=\"reference internal\" href=\"/en/1.8/ref/urls/#django.conf.urls.include\" title=\"django.conf.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a>, leaving you\nwith:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>mysite/urls.py</code></figcaption><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=\"mysite/urls.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">include</span><span class=\"p\">,</span> <span class=\"n\">url</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">admin</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^polls/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"s1\">&#39;polls.urls&#39;</span><span class=\"p\">)),</span>\n    <span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^admin/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"n\">admin</span><span class=\"o\">.</span><span class=\"n\">site</span><span class=\"o\">.</span><span class=\"n\">urls</span><span class=\"p\">)),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<aside class=\"admonition-doesn-t-match-what-you-see admonition\">\n<p class=\"admonition-title\">Doesn’t match what you see?</p>\n<p>If you’re seeing <code class=\"docutils literal notranslate\"><span class=\"pre\">admin.autodiscover()</span></code> before the definition of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code>, you’re probably using a version of Django that doesn’t\nmatch this tutorial version.  You’ll want to either switch to the older\ntutorial or the newer Django version.</p>\n</aside>\n<p>You have now wired an <code class=\"docutils literal notranslate\"><span class=\"pre\">index</span></code> view into the URLconf. Go to\n<a class=\"reference external\" href=\"http://localhost:8000/polls/\">http://localhost:8000/polls/</a> in your browser, and you should see the text\n“<em>Hello, world. You’re at the polls index.</em>”, which you defined in the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">index</span></code> view.</p>\n<p>The <a class=\"reference internal\" href=\"/en/1.8/ref/urls/#django.conf.urls.url\" title=\"django.conf.urls.url\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">url()</span></code></a> function is passed four arguments, two\nrequired: <code class=\"docutils literal notranslate\"><span class=\"pre\">regex</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">view</span></code>, and two optional: <code class=\"docutils literal notranslate\"><span class=\"pre\">kwargs</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code>.\nAt this point, it’s worth reviewing what these arguments are for.</p>\n<section id=\"url-argument-regex\">\n<h3><a class=\"reference internal\" href=\"/en/1.8/ref/urls/#django.conf.urls.url\" title=\"django.conf.urls.url\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">url()</span></code></a> argument: regex<a class=\"heading-anchor\" href=\"#url-argument-regex\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The term “regex” is a commonly used short form meaning “regular expression”,\nwhich is a syntax for matching patterns in strings, or in this case, url\npatterns. Django starts at the first regular expression and makes its way down\nthe list,  comparing the requested URL against each regular expression until it\nfinds one that matches.</p>\n<p>Note that these regular expressions do not search GET and POST parameters, or\nthe domain name. For example, in a request to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">http://www.example.com/myapp/</span></code>, the URLconf will look for <code class=\"docutils literal notranslate\"><span class=\"pre\">myapp/</span></code>. In a\nrequest to <code class=\"docutils literal notranslate\"><span class=\"pre\">http://www.example.com/myapp/?page=3</span></code>, the URLconf will also\nlook for <code class=\"docutils literal notranslate\"><span class=\"pre\">myapp/</span></code>.</p>\n<p>If you need help with regular expressions, see <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Regular_expression\">Wikipedia’s entry</a> and the\ndocumentation of the <a class=\"reference external\" href=\"https://docs.python.org/3/library/re.html#module-re\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">re</span></code></a> module. Also, the O’Reilly book “Mastering\nRegular Expressions” by Jeffrey Friedl is fantastic. In practice, however,\nyou don’t need to be an expert on regular expressions, as you really only need\nto know how to capture simple patterns. In fact, complex regexes can have poor\nlookup performance, so you probably shouldn’t rely on the full power of regexes.</p>\n<p>Finally, a performance note: these regular expressions are compiled the first\ntime the URLconf module is loaded. They’re super fast (as long as the lookups\naren’t too complex as noted above).</p>\n</section>\n<section id=\"url-argument-view\">\n<h3><a class=\"reference internal\" href=\"/en/1.8/ref/urls/#django.conf.urls.url\" title=\"django.conf.urls.url\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">url()</span></code></a> argument: view<a class=\"heading-anchor\" href=\"#url-argument-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When Django finds a regular expression match, Django calls the specified view\nfunction, with an <a class=\"reference internal\" href=\"/en/1.8/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> object as the first\nargument and any “captured” values from the regular expression as other\narguments. If the regex uses simple captures, values are passed as positional\narguments; if it uses named captures, values are passed as keyword arguments.\nWe’ll give an example of this in a bit.</p>\n</section>\n<section id=\"url-argument-kwargs\">\n<h3><a class=\"reference internal\" href=\"/en/1.8/ref/urls/#django.conf.urls.url\" title=\"django.conf.urls.url\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">url()</span></code></a> argument: kwargs<a class=\"heading-anchor\" href=\"#url-argument-kwargs\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Arbitrary keyword arguments can be passed in a dictionary to the target view. We\naren’t going to use this feature of Django in the tutorial.</p>\n</section>\n<section id=\"url-argument-name\">\n<h3><a class=\"reference internal\" href=\"/en/1.8/ref/urls/#django.conf.urls.url\" title=\"django.conf.urls.url\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">url()</span></code></a> argument: name<a class=\"heading-anchor\" href=\"#url-argument-name\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Naming your URL lets you refer to it unambiguously from elsewhere in Django\nespecially templates. This powerful feature allows you to make  global changes\nto the url patterns of your project while only touching a single file.</p>\n</section>\n</section>\n<section id=\"writing-more-views\">\n<h2>Writing more views<a class=\"heading-anchor\" href=\"#writing-more-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Now let’s add a few more views to <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/views.py</span></code>. These views are\nslightly different, because they take an argument:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/views.py</code></figcaption><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=\"polls/views.py\"><code><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">detail</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"n\">question_id</span><span class=\"p\">):</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"s2\">&quot;You&#39;re looking at question </span><span class=\"si\">%s</span><span class=\"s2\">.&quot;</span> <span class=\"o\">%</span> <span class=\"n\">question_id</span><span class=\"p\">)</span>\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">results</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"n\">question_id</span><span class=\"p\">):</span>\n    <span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;You&#39;re looking at the results of question </span><span class=\"si\">%s</span><span class=\"s2\">.&quot;</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"n\">response</span> <span class=\"o\">%</span> <span class=\"n\">question_id</span><span class=\"p\">)</span>\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">vote</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"n\">question_id</span><span class=\"p\">):</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"s2\">&quot;You&#39;re voting on question </span><span class=\"si\">%s</span><span class=\"s2\">.&quot;</span> <span class=\"o\">%</span> <span class=\"n\">question_id</span><span class=\"p\">)</span>\n</code></pre></figure>\n<p>Wire these new views into the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls.urls</span></code> module by adding the following\n<a class=\"reference internal\" href=\"/en/1.8/ref/urls/#django.conf.urls.url\" title=\"django.conf.urls.url\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">url()</span></code></a> calls:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/urls.py</code></figcaption><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=\"polls/urls.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">url</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"c1\"># ex: /polls/</span>\n    <span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^$&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">index</span><span class=\"p\">,</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;index&#39;</span><span class=\"p\">),</span>\n    <span class=\"c1\"># ex: /polls/5/</span>\n    <span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^(?P&lt;question_id&gt;[0-9]+)/$&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">detail</span><span class=\"p\">,</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;detail&#39;</span><span class=\"p\">),</span>\n    <span class=\"c1\"># ex: /polls/5/results/</span>\n    <span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^(?P&lt;question_id&gt;[0-9]+)/results/$&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">results</span><span class=\"p\">,</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;results&#39;</span><span class=\"p\">),</span>\n    <span class=\"c1\"># ex: /polls/5/vote/</span>\n    <span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^(?P&lt;question_id&gt;[0-9]+)/vote/$&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">vote</span><span class=\"p\">,</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;vote&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>Take a look in your browser, at “/polls/34/”. It’ll run the <code class=\"docutils literal notranslate\"><span class=\"pre\">detail()</span></code>\nmethod and display whatever ID you provide in the URL. Try\n“/polls/34/results/” and “/polls/34/vote/” too – these will display the\nplaceholder results and voting pages.</p>\n<p>When somebody requests a page from your Web site – say, “/polls/34/”, Django\nwill load the <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite.urls</span></code> Python module because it’s pointed to by the\n<a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-ROOT_URLCONF\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ROOT_URLCONF</span></code></a> setting. It finds the variable named <code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code>\nand traverses the regular expressions in order. The\n<a class=\"reference internal\" href=\"/en/1.8/ref/urls/#django.conf.urls.include\" title=\"django.conf.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a> functions we are using simply reference\nother URLconfs. Note that the regular expressions for the\n<a class=\"reference internal\" href=\"/en/1.8/ref/urls/#django.conf.urls.include\" title=\"django.conf.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a> functions don’t have a <code class=\"docutils literal notranslate\"><span class=\"pre\">$</span></code> (end-of-string\nmatch character) but rather a trailing slash. Whenever Django encounters\n<a class=\"reference internal\" href=\"/en/1.8/ref/urls/#django.conf.urls.include\" title=\"django.conf.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a>, it chops off whatever part of the URL\nmatched up to that point and sends the remaining string to the included\nURLconf for further processing.</p>\n<p>The idea behind <a class=\"reference internal\" href=\"/en/1.8/ref/urls/#django.conf.urls.include\" title=\"django.conf.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a> is to make it easy to\nplug-and-play URLs. Since polls are in their own URLconf\n(<code class=\"docutils literal notranslate\"><span class=\"pre\">polls/urls.py</span></code>), they can be placed under “/polls/”, or under\n“/fun_polls/”, or under “/content/polls/”, or any other path root, and the\napp will still work.</p>\n<p>Here’s what happens if a user goes to “/polls/34/” in this system:</p>\n<ul>\n<li><p>Django will find the match at <code class=\"docutils literal notranslate\"><span class=\"pre\">'^polls/'</span></code></p></li>\n<li><p>Then, Django will strip off the matching text (<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;polls/&quot;</span></code>) and send the\nremaining text – <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;34/&quot;</span></code> – to the ‘polls.urls’ URLconf for\nfurther processing which matches <code class=\"docutils literal notranslate\"><span class=\"pre\">r'^(?P&lt;question_id&gt;[0-9]+)/$'</span></code> resulting in a\ncall to the <code class=\"docutils literal notranslate\"><span class=\"pre\">detail()</span></code> view like so:</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=\"n\">detail</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"o\">=&lt;</span><span class=\"n\">HttpRequest</span> <span class=\"nb\">object</span><span class=\"o\">&gt;</span><span class=\"p\">,</span> <span class=\"n\">question_id</span><span class=\"o\">=</span><span class=\"s1\">&#39;34&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n</li>\n</ul>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">question_id='34'</span></code> part comes from <code class=\"docutils literal notranslate\"><span class=\"pre\">(?P&lt;question_id&gt;[0-9]+)</span></code>. Using parentheses\naround a pattern “captures” the text matched by that pattern and sends it as an\nargument to the view function; <code class=\"docutils literal notranslate\"><span class=\"pre\">?P&lt;question_id&gt;</span></code> defines the name that will\nbe used to identify the matched pattern; and <code class=\"docutils literal notranslate\"><span class=\"pre\">[0-9]+</span></code> is a regular expression to\nmatch a sequence of digits (i.e., a number).</p>\n<p>Because the URL patterns are regular expressions, there really is no limit on\nwhat you can do with them. And there’s no need to add URL cruft such as\n<code class=\"docutils literal notranslate\"><span class=\"pre\">.html</span></code> – unless you want to, in which case you can do something like\nthis:</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=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^polls/latest\\.html$&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">index</span><span class=\"p\">),</span>\n</code></pre></div>\n<p>But, don’t do that. It’s silly.</p>\n</section>\n<section id=\"write-views-that-actually-do-something\">\n<h2>Write views that actually do something<a class=\"heading-anchor\" href=\"#write-views-that-actually-do-something\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Each view is responsible for doing one of two things: returning an\n<a class=\"reference internal\" href=\"/en/1.8/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> object containing the content for the\nrequested page, or raising an exception such as <a class=\"reference internal\" href=\"/en/1.8/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>. The\nrest is up to you.</p>\n<p>Your view can read records from a database, or not. It can use a template\nsystem such as Django’s – or a third-party Python template system – or not.\nIt can generate a PDF file, output XML, create a ZIP file on the fly, anything\nyou want, using whatever Python libraries you want.</p>\n<p>All Django wants is that <a class=\"reference internal\" href=\"/en/1.8/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a>. Or an exception.</p>\n<p>Because it’s convenient, let’s use Django’s own database API, which we covered\nin <a class=\"reference internal\" href=\"/en/1.8/intro/tutorial01/\"><span class=\"doc\">Tutorial 1</span></a>. Here’s one stab at a new <code class=\"docutils literal notranslate\"><span class=\"pre\">index()</span></code>\nview, which displays the latest 5 poll questions in the system, separated by\ncommas, according to publication date:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/views.py</code></figcaption><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=\"polls/views.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HttpResponse</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Question</span>\n\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">index</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"n\">latest_question_list</span> <span class=\"o\">=</span> <span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">order_by</span><span class=\"p\">(</span><span class=\"s1\">&#39;-pub_date&#39;</span><span class=\"p\">)[:</span><span class=\"mi\">5</span><span class=\"p\">]</span>\n    <span class=\"n\">output</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;, &#39;</span><span class=\"o\">.</span><span class=\"n\">join</span><span class=\"p\">([</span><span class=\"n\">p</span><span class=\"o\">.</span><span class=\"n\">question_text</span> <span class=\"k\">for</span> <span class=\"n\">p</span> <span class=\"ow\">in</span> <span class=\"n\">latest_question_list</span><span class=\"p\">])</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"n\">output</span><span class=\"p\">)</span>\n\n<span class=\"c1\"># Leave the rest of the views (detail, results, vote) unchanged</span>\n</code></pre></figure>\n<p>There’s a problem here, though: the page’s design is hard-coded in the view. If\nyou want to change the way the page looks, you’ll have to edit this Python code.\nSo let’s use Django’s template system to separate the design from Python by\ncreating a template that the view can use.</p>\n<p>First, create a directory called <code class=\"docutils literal notranslate\"><span class=\"pre\">templates</span></code> in your <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> directory.\nDjango will look for templates in there.</p>\n<p>Your project’s <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a> setting describes how Django will load and\nrender templates. The default settings file configures a <code class=\"docutils literal notranslate\"><span class=\"pre\">DjangoTemplates</span></code>\nbackend whose <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-APP_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APP_DIRS</span></code></a> option is set to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>. By convention <code class=\"docutils literal notranslate\"><span class=\"pre\">DjangoTemplates</span></code> looks for a “templates”\nsubdirectory in each of the <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a>. This is how Django\nknows to find the polls templates even though we didn’t modify the\n<a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DIRS</span></code></a> option, as we did in <a class=\"reference internal\" href=\"/en/1.8/intro/tutorial02/#ref-customizing-your-projects-templates\"><span class=\"std std-ref\">Tutorial 2</span></a>.</p>\n<aside class=\"admonition-organizing-templates admonition\">\n<p class=\"admonition-title\">Organizing templates</p>\n<p>We <em>could</em> have all our templates together, in one big templates directory,\nand it would work perfectly well. However, this template belongs to the\npolls application, so unlike the admin template we created in the previous\ntutorial, we’ll put this one in the application’s template directory\n(<code class=\"docutils literal notranslate\"><span class=\"pre\">polls/templates</span></code>) rather than the project’s (<code class=\"docutils literal notranslate\"><span class=\"pre\">templates</span></code>). We’ll\ndiscuss in more detail in the <a class=\"reference internal\" href=\"/en/1.8/intro/reusable-apps/\"><span class=\"doc\">reusable apps tutorial</span></a> <em>why</em> we do this.</p>\n</aside>\n<p>Within the <code class=\"docutils literal notranslate\"><span class=\"pre\">templates</span></code> directory you have just created, create another\ndirectory called <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code>, and within that create a file called\n<code class=\"docutils literal notranslate\"><span class=\"pre\">index.html</span></code>. In other words, your template should be at\n<code class=\"docutils literal notranslate\"><span class=\"pre\">polls/templates/polls/index.html</span></code>. Because of how the <code class=\"docutils literal notranslate\"><span class=\"pre\">app_directories</span></code>\ntemplate loader works as described above, you can refer to this template within\nDjango simply as <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/index.html</span></code>.</p>\n<aside class=\"admonition-template-namespacing admonition\">\n<p class=\"admonition-title\">Template namespacing</p>\n<p>Now we <em>might</em> be able to get away with putting our templates directly in\n<code class=\"docutils literal notranslate\"><span class=\"pre\">polls/templates</span></code> (rather than creating another <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> subdirectory),\nbut it would actually be a bad idea. Django will choose the first template\nit finds whose name matches, and if you had a template with the same name\nin a <em>different</em> application, Django would be unable to distinguish between\nthem. We need to be able to point Django at the right one, and the easiest\nway to ensure this is by <em>namespacing</em> them. That is, by putting those\ntemplates inside <em>another</em> directory named for the application itself.</p>\n</aside>\n<p>Put the following code in that template:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"html+django\"><figcaption class=\"code-block-caption\"><code>polls/templates/polls/index.html</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"polls/templates/polls/index.html\"><code><span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">latest_question_list</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">question</span> <span class=\"k\">in</span> <span class=\"nv\">latest_question_list</span> <span class=\"cp\">%}</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;/polls/</span><span class=\"cp\">{{</span> <span class=\"nv\">question.id</span> <span class=\"cp\">}}</span><span class=\"s\">/&quot;</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">question.question_text</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">else</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>No polls are available.<span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></figure>\n<p>Now let’s update our <code class=\"docutils literal notranslate\"><span class=\"pre\">index</span></code> view in <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/views.py</span></code> to use the template:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/views.py</code></figcaption><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=\"polls/views.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HttpResponse</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">loader</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Question</span>\n\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">index</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"n\">latest_question_list</span> <span class=\"o\">=</span> <span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">order_by</span><span class=\"p\">(</span><span class=\"s1\">&#39;-pub_date&#39;</span><span class=\"p\">)[:</span><span class=\"mi\">5</span><span class=\"p\">]</span>\n    <span class=\"n\">template</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=\"s1\">&#39;polls/index.html&#39;</span><span class=\"p\">)</span>\n    <span class=\"n\">context</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n        <span class=\"s1\">&#39;latest_question_list&#39;</span><span class=\"p\">:</span> <span class=\"n\">latest_question_list</span><span class=\"p\">,</span>\n    <span class=\"p\">}</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"n\">template</span><span class=\"o\">.</span><span class=\"n\">render</span><span class=\"p\">(</span><span class=\"n\">context</span><span class=\"p\">,</span> <span class=\"n\">request</span><span class=\"p\">))</span>\n</code></pre></figure>\n<p>That code loads the template called  <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/index.html</span></code> and passes it a\ncontext. The context is a dictionary mapping template variable names to Python\nobjects.</p>\n<p>Load the page by pointing your browser at “/polls/”, and you should see a\nbulleted-list containing the “What’s up” question from Tutorial 1. The link points\nto the question’s detail page.</p>\n<section id=\"a-shortcut-render\">\n<h3>A shortcut: <a class=\"reference internal\" href=\"/en/1.8/topics/http/shortcuts/#django.shortcuts.render\" title=\"django.shortcuts.render\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">render()</span></code></a><a class=\"heading-anchor\" href=\"#a-shortcut-render\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>It’s a very common idiom to load a template, fill a context and return an\n<a class=\"reference internal\" href=\"/en/1.8/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> object with the result of the rendered\ntemplate. Django provides a shortcut. Here’s the full <code class=\"docutils literal notranslate\"><span class=\"pre\">index()</span></code> view,\nrewritten:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/views.py</code></figcaption><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=\"polls/views.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.shortcuts</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">render</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Question</span>\n\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">index</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"n\">latest_question_list</span> <span class=\"o\">=</span> <span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">order_by</span><span class=\"p\">(</span><span class=\"s1\">&#39;-pub_date&#39;</span><span class=\"p\">)[:</span><span class=\"mi\">5</span><span class=\"p\">]</span>\n    <span class=\"n\">context</span> <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"s1\">&#39;latest_question_list&#39;</span><span class=\"p\">:</span> <span class=\"n\">latest_question_list</span><span class=\"p\">}</span>\n    <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=\"s1\">&#39;polls/index.html&#39;</span><span class=\"p\">,</span> <span class=\"n\">context</span><span class=\"p\">)</span>\n</code></pre></figure>\n<p>Note that once we’ve done this in all these views, we no longer need to import\n<a class=\"reference internal\" href=\"/en/1.8/topics/templates/#module-django.template.loader\" title=\"django.template.loader\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">loader</span></code></a> and <a class=\"reference internal\" href=\"/en/1.8/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> (you’ll\nwant to keep <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code> if you still have the stub methods for <code class=\"docutils literal notranslate\"><span class=\"pre\">detail</span></code>,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">results</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">vote</span></code>).</p>\n<p>The <a class=\"reference internal\" href=\"/en/1.8/topics/http/shortcuts/#django.shortcuts.render\" title=\"django.shortcuts.render\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">render()</span></code></a> function takes the request object as its\nfirst argument, a template name as its second argument and a dictionary as its\noptional third argument. It returns an <a class=\"reference internal\" href=\"/en/1.8/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a>\nobject of the given template rendered with the given context.</p>\n</section>\n</section>\n<section id=\"raising-a-404-error\">\n<h2>Raising a 404 error<a class=\"heading-anchor\" href=\"#raising-a-404-error\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Now, let’s tackle the question detail view – the page that displays the question text\nfor a given poll. Here’s the view:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/views.py</code></figcaption><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=\"polls/views.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Http404</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.shortcuts</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">render</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Question</span>\n<span class=\"c1\"># ...</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">detail</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"n\">question_id</span><span class=\"p\">):</span>\n    <span class=\"k\">try</span><span class=\"p\">:</span>\n        <span class=\"n\">question</span> <span class=\"o\">=</span> <span class=\"n\">Question</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=\"n\">question_id</span><span class=\"p\">)</span>\n    <span class=\"k\">except</span> <span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">DoesNotExist</span><span class=\"p\">:</span>\n        <span class=\"k\">raise</span> <span class=\"n\">Http404</span><span class=\"p\">(</span><span class=\"s2\">&quot;Question does not exist&quot;</span><span class=\"p\">)</span>\n    <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=\"s1\">&#39;polls/detail.html&#39;</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;question&#39;</span><span class=\"p\">:</span> <span class=\"n\">question</span><span class=\"p\">})</span>\n</code></pre></figure>\n<p>The new concept here: The view raises the <a class=\"reference internal\" href=\"/en/1.8/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> exception\nif a question with the requested ID doesn’t exist.</p>\n<p>We’ll discuss what you could put in that <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/detail.html</span></code> template a bit\nlater, but if you’d like to quickly get the above example working, a file\ncontaining just:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"html+django\"><figcaption class=\"code-block-caption\"><code>polls/templates/polls/detail.html</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"polls/templates/polls/detail.html\"><code><span class=\"cp\">{{</span> <span class=\"nv\">question</span> <span class=\"cp\">}}</span>\n</code></pre></figure>\n<p>will get you started for now.</p>\n<section id=\"a-shortcut-get-object-or-404\">\n<h3>A shortcut: <a class=\"reference internal\" href=\"/en/1.8/topics/http/shortcuts/#django.shortcuts.get_object_or_404\" title=\"django.shortcuts.get_object_or_404\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">get_object_or_404()</span></code></a><a class=\"heading-anchor\" href=\"#a-shortcut-get-object-or-404\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>It’s a very common idiom to use <a class=\"reference internal\" href=\"/en/1.8/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\"><span class=\"pre\">get()</span></code></a>\nand raise <a class=\"reference internal\" href=\"/en/1.8/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> if the object doesn’t exist. Django\nprovides a shortcut. Here’s the <code class=\"docutils literal notranslate\"><span class=\"pre\">detail()</span></code> view, rewritten:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/views.py</code></figcaption><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=\"polls/views.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.shortcuts</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_object_or_404</span><span class=\"p\">,</span> <span class=\"n\">render</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Question</span>\n<span class=\"c1\"># ...</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">detail</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"n\">question_id</span><span class=\"p\">):</span>\n    <span class=\"n\">question</span> <span class=\"o\">=</span> <span class=\"n\">get_object_or_404</span><span class=\"p\">(</span><span class=\"n\">Question</span><span class=\"p\">,</span> <span class=\"n\">pk</span><span class=\"o\">=</span><span class=\"n\">question_id</span><span class=\"p\">)</span>\n    <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=\"s1\">&#39;polls/detail.html&#39;</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;question&#39;</span><span class=\"p\">:</span> <span class=\"n\">question</span><span class=\"p\">})</span>\n</code></pre></figure>\n<p>The <a class=\"reference internal\" href=\"/en/1.8/topics/http/shortcuts/#django.shortcuts.get_object_or_404\" title=\"django.shortcuts.get_object_or_404\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">get_object_or_404()</span></code></a> function takes a Django model\nas its first argument and an arbitrary number of keyword arguments, which it\npasses to the <a class=\"reference internal\" href=\"/en/1.8/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\"><span class=\"pre\">get()</span></code></a> function of the\nmodel’s manager. It raises <a class=\"reference internal\" href=\"/en/1.8/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> if the object doesn’t\nexist.</p>\n<aside class=\"admonition-philosophy admonition\">\n<p class=\"admonition-title\">Philosophy</p>\n<p>Why do we use a helper function <a class=\"reference internal\" href=\"/en/1.8/topics/http/shortcuts/#django.shortcuts.get_object_or_404\" title=\"django.shortcuts.get_object_or_404\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">get_object_or_404()</span></code></a>\ninstead of automatically catching the\n<a class=\"reference internal\" href=\"/en/1.8/ref/exceptions/#django.core.exceptions.ObjectDoesNotExist\" title=\"django.core.exceptions.ObjectDoesNotExist\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">ObjectDoesNotExist</span></code></a> exceptions at a higher\nlevel, or having the model API raise <a class=\"reference internal\" href=\"/en/1.8/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> instead of\n<a class=\"reference internal\" href=\"/en/1.8/ref/exceptions/#django.core.exceptions.ObjectDoesNotExist\" title=\"django.core.exceptions.ObjectDoesNotExist\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">ObjectDoesNotExist</span></code></a>?</p>\n<p>Because that would couple the model layer to the view layer. One of the\nforemost design goals of Django is to maintain loose coupling. Some\ncontrolled coupling is introduced in the <a class=\"reference internal\" href=\"/en/1.8/topics/http/shortcuts/#module-django.shortcuts\" title=\"django.shortcuts: Convenience shortcuts that span multiple levels of Django's MVC stack.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.shortcuts</span></code></a> module.</p>\n</aside>\n<p>There’s also a <a class=\"reference internal\" href=\"/en/1.8/topics/http/shortcuts/#django.shortcuts.get_list_or_404\" title=\"django.shortcuts.get_list_or_404\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">get_list_or_404()</span></code></a> function, which works\njust as <a class=\"reference internal\" href=\"/en/1.8/topics/http/shortcuts/#django.shortcuts.get_object_or_404\" title=\"django.shortcuts.get_object_or_404\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">get_object_or_404()</span></code></a> – except using\n<a class=\"reference internal\" href=\"/en/1.8/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\"><span class=\"pre\">filter()</span></code></a> instead of\n<a class=\"reference internal\" href=\"/en/1.8/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\"><span class=\"pre\">get()</span></code></a>. It raises\n<a class=\"reference internal\" href=\"/en/1.8/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> if the list is empty.</p>\n</section>\n</section>\n<section id=\"use-the-template-system\">\n<h2>Use the template system<a class=\"heading-anchor\" href=\"#use-the-template-system\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Back to the <code class=\"docutils literal notranslate\"><span class=\"pre\">detail()</span></code> view for our poll application. Given the context\nvariable <code class=\"docutils literal notranslate\"><span class=\"pre\">question</span></code>, here’s what the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/detail.html</span></code> template might look\nlike:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"html+django\"><figcaption class=\"code-block-caption\"><code>polls/templates/polls/detail.html</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"polls/templates/polls/detail.html\"><code><span class=\"p\">&lt;</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">question.question_text</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">choice</span> <span class=\"k\">in</span> <span class=\"nv\">question.choice_set.all</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">choice.choice_text</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n</code></pre></figure>\n<p>The template system uses dot-lookup syntax to access variable attributes. In\nthe example of <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">question.question_text</span> <span class=\"pre\">}}</span></code>, first Django does a dictionary lookup\non the object <code class=\"docutils literal notranslate\"><span class=\"pre\">question</span></code>. Failing that, it tries an attribute lookup – which\nworks, in this case. If attribute lookup had failed, it would’ve tried a\nlist-index lookup.</p>\n<p>Method-calling happens in the <a class=\"reference internal\" href=\"/en/1.8/ref/templates/builtins/#std-templatetag-for\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">for</span> <span class=\"pre\">%}</span></code></a> loop:\n<code class=\"docutils literal notranslate\"><span class=\"pre\">question.choice_set.all</span></code> is interpreted as the Python code\n<code class=\"docutils literal notranslate\"><span class=\"pre\">question.choice_set.all()</span></code>, which returns an iterable of <code class=\"docutils literal notranslate\"><span class=\"pre\">Choice</span></code> objects and is\nsuitable for use in the <a class=\"reference internal\" href=\"/en/1.8/ref/templates/builtins/#std-templatetag-for\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">for</span> <span class=\"pre\">%}</span></code></a> tag.</p>\n<p>See the <a class=\"reference internal\" href=\"/en/1.8/topics/templates/\"><span class=\"doc\">template guide</span></a> for more about templates.</p>\n</section>\n<section id=\"removing-hardcoded-urls-in-templates\">\n<h2>Removing hardcoded URLs in templates<a class=\"heading-anchor\" href=\"#removing-hardcoded-urls-in-templates\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Remember, when we wrote the link to a question in the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/index.html</span></code>\ntemplate, the link was partially hardcoded like this:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;/polls/</span><span class=\"cp\">{{</span> <span class=\"nv\">question.id</span> <span class=\"cp\">}}</span><span class=\"s\">/&quot;</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">question.question_text</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>The problem with this hardcoded, tightly-coupled approach is that it becomes\nchallenging to change URLs on projects with a lot of templates. However, since\nyou defined the name argument in the <a class=\"reference internal\" href=\"/en/1.8/ref/urls/#django.conf.urls.url\" title=\"django.conf.urls.url\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">url()</span></code></a> functions in\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">polls.urls</span></code> module, you can remove a reliance on specific URL paths\ndefined in your url configurations by using the <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">url</span> <span class=\"pre\">%}</span></code> template tag:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">url</span> <span class=\"s1\">&#39;detail&#39;</span> <span class=\"nv\">question.id</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">question.question_text</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>The way this works is by looking up the URL definition as specified in the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">polls.urls</span></code> module. You can see exactly where the URL name of ‘detail’ is\ndefined below:</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=\"o\">...</span>\n<span class=\"c1\"># the &#39;name&#39; value as called by the {% url %} template tag</span>\n<span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^(?P&lt;question_id&gt;[0-9]+)/$&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">detail</span><span class=\"p\">,</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;detail&#39;</span><span class=\"p\">),</span>\n<span class=\"o\">...</span>\n</code></pre></div>\n<p>If you want to change the URL of the polls detail view to something else,\nperhaps to something like <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/specifics/12/</span></code> instead of doing it in the\ntemplate (or templates) you would change it in <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/urls.py</span></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=\"o\">...</span>\n<span class=\"c1\"># added the word &#39;specifics&#39;</span>\n<span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^specifics/(?P&lt;question_id&gt;[0-9]+)/$&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">detail</span><span class=\"p\">,</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;detail&#39;</span><span class=\"p\">),</span>\n<span class=\"o\">...</span>\n</code></pre></div>\n</section>\n<section id=\"namespacing-url-names\">\n<h2>Namespacing URL names<a class=\"heading-anchor\" href=\"#namespacing-url-names\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The tutorial project has just one app, <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code>. In real Django projects,\nthere might be five, ten, twenty apps or more. How does Django differentiate\nthe URL names between them? For example, the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> app has a <code class=\"docutils literal notranslate\"><span class=\"pre\">detail</span></code>\nview, and so might an app on the same project that is for a blog. How does one\nmake it so that Django knows which app view to create for a url when using the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">url</span> <span class=\"pre\">%}</span></code> template tag?</p>\n<p>The answer is to add namespaces to your root URLconf. In the <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite/urls.py</span></code>\nfile, go ahead and change it to include namespacing:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>mysite/urls.py</code></figcaption><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=\"mysite/urls.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">include</span><span class=\"p\">,</span> <span class=\"n\">url</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">admin</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^polls/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"s1\">&#39;polls.urls&#39;</span><span class=\"p\">,</span> <span class=\"n\">namespace</span><span class=\"o\">=</span><span class=\"s2\">&quot;polls&quot;</span><span class=\"p\">)),</span>\n    <span class=\"n\">url</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^admin/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"n\">admin</span><span class=\"o\">.</span><span class=\"n\">site</span><span class=\"o\">.</span><span class=\"n\">urls</span><span class=\"p\">)),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>Now change your <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/index.html</span></code> template from:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"html+django\"><figcaption class=\"code-block-caption\"><code>polls/templates/polls/index.html</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"polls/templates/polls/index.html\"><code><span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">url</span> <span class=\"s1\">&#39;detail&#39;</span> <span class=\"nv\">question.id</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">question.question_text</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n</code></pre></figure>\n<p>to point at the namespaced detail view:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"html+django\"><figcaption class=\"code-block-caption\"><code>polls/templates/polls/index.html</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"polls/templates/polls/index.html\"><code><span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">url</span> <span class=\"s1\">&#39;polls:detail&#39;</span> <span class=\"nv\">question.id</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">question.question_text</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n</code></pre></figure>\n<p>When you’re comfortable with writing views, read <a class=\"reference internal\" href=\"/en/1.8/intro/tutorial04/\"><span class=\"doc\">part 4 of this tutorial</span></a> to learn about simple form processing and generic views.</p>\n</section>","rootId":"writing-your-first-django-app-part-3","toc":[{"title":"Philosophy","anchor":"philosophy","children":[]},{"title":"Write your first view","anchor":"write-your-first-view","children":[{"title":"url() argument: regex","anchor":"url-argument-regex","children":[]},{"title":"url() argument: view","anchor":"url-argument-view","children":[]},{"title":"url() argument: kwargs","anchor":"url-argument-kwargs","children":[]},{"title":"url() argument: name","anchor":"url-argument-name","children":[]}]},{"title":"Writing more views","anchor":"writing-more-views","children":[]},{"title":"Write views that actually do something","anchor":"write-views-that-actually-do-something","children":[{"title":"A shortcut: render()","anchor":"a-shortcut-render","children":[]}]},{"title":"Raising a 404 error","anchor":"raising-a-404-error","children":[{"title":"A shortcut: get_object_or_404()","anchor":"a-shortcut-get-object-or-404","children":[]}]},{"title":"Use the template system","anchor":"use-the-template-system","children":[]},{"title":"Removing hardcoded URLs in templates","anchor":"removing-hardcoded-urls-in-templates","children":[]},{"title":"Namespacing URL names","anchor":"namespacing-url-names","children":[]}],"breadcrumbs":[{"docname":"intro/index","title":"Getting started","url":"/en/1.8/intro/"}],"prev":{"docname":"intro/tutorial02","title":"Writing your first Django app, part 2","url":"/en/1.8/intro/tutorial02/"},"next":{"docname":"intro/tutorial04","title":"Writing your first Django app, part 4","url":"/en/1.8/intro/tutorial04/"},"formats":{"html":"/en/1.8/intro/tutorial03/","markdown":"/en/1.8/intro/tutorial03.md","json":"/en/1.8/intro/tutorial03.json"},"source":"https://github.com/django/django/blob/stable/1.8.x/docs/intro/tutorial03.txt","official":"https://docs.djangoproject.com/en/1.8/intro/tutorial03/","inVersions":["dev","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","1.8"],"inLocales":["en"]}