| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|---|
| 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
|
|---|
| 3 | <meta http-equiv="Content-type" content="text/html; charset=UTF-8">
|
|---|
| 4 | <meta http-equiv="Content-Language" content="en-us">
|
|---|
| 5 | <title>Writing your first Django app, part 3 | Django documentation | Django</title>
|
|---|
| 6 | <meta name="ROBOTS" content="ALL">
|
|---|
| 7 | <meta http-equiv="imagetoolbar" content="no">
|
|---|
| 8 | <meta name="MSSmartTagsPreventParsing" content="true">
|
|---|
| 9 | <meta name="Copyright" content="Django Software Foundation">
|
|---|
| 10 | <meta name="keywords" content="Python, Django, framework, open-source">
|
|---|
| 11 | <meta name="description" content="Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.">
|
|---|
| 12 |
|
|---|
| 13 | <link href="Writing%20your%20first%20Django%20app,%20part%203%20%7C%20Django%20documentation%20%7C%20Django_files/base.css" rel="stylesheet" type="text/css" media="screen">
|
|---|
| 14 |
|
|---|
| 15 | <link href="Writing%20your%20first%20Django%20app,%20part%203%20%7C%20Django%20documentation%20%7C%20Django_files/print.css" rel="stylesheet" type="text/css" media="print">
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | <link rel="stylesheet" href="Writing%20your%20first%20Django%20app,%20part%203%20%7C%20Django%20documentation%20%7C%20Django_files/docs.css" type="text/css">
|
|---|
| 19 | <link rel="stylesheet" href="Writing%20your%20first%20Django%20app,%20part%203%20%7C%20Django%20documentation%20%7C%20Django_files/pygments.css" type="text/css">
|
|---|
| 20 |
|
|---|
| 21 | </head>
|
|---|
| 22 | <body id="documentation" class="default">
|
|---|
| 23 | <div id="container">
|
|---|
| 24 | <div id="header">
|
|---|
| 25 | <h1 id="logo"><a href="https://www.djangoproject.com/"><img src="Writing%20your%20first%20Django%20app,%20part%203%20%7C%20Django%20documentation%20%7C%20Django_files/hdr_logo.gif" alt="Django"></a></h1>
|
|---|
| 26 | <ul id="nav-global">
|
|---|
| 27 | <li id="nav-homepage"><a href="https://www.djangoproject.com/">Home</a></li>
|
|---|
| 28 | <li id="nav-download"><a href="https://www.djangoproject.com/download/">Download</a></li>
|
|---|
| 29 | <li id="nav-documentation"><a href="https://docs.djangoproject.com/">Documentation</a></li>
|
|---|
| 30 | <li id="nav-weblog"><a href="https://www.djangoproject.com/weblog/">Weblog</a></li>
|
|---|
| 31 | <li id="nav-community"><a href="https://www.djangoproject.com/community/">Community</a></li>
|
|---|
| 32 | <li id="nav-code"><a href="https://code.djangoproject.com/">Code</a></li>
|
|---|
| 33 | </ul>
|
|---|
| 34 | </div>
|
|---|
| 35 | <!-- END Header -->
|
|---|
| 36 | <div id="billboard">
|
|---|
| 37 | <h2><a href="https://docs.djangoproject.com/en/dev/">Django documentation</a></h2>
|
|---|
| 38 | </div>
|
|---|
| 39 | <div id="columnwrap">
|
|---|
| 40 |
|
|---|
| 41 | <div id="content-main">
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | <ul id="doc-versions">
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | <li class="other">
|
|---|
| 51 |
|
|---|
| 52 | <a href="https://docs.djangoproject.com/en/1.0/intro/tutorial03/">1.0</a>
|
|---|
| 53 |
|
|---|
| 54 | </li>
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 | <li class="other">
|
|---|
| 59 |
|
|---|
| 60 | <a href="https://docs.djangoproject.com/en/1.1/intro/tutorial03/">1.1</a>
|
|---|
| 61 |
|
|---|
| 62 | </li>
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 | <li class="other">
|
|---|
| 67 |
|
|---|
| 68 | <a href="https://docs.djangoproject.com/en/1.2/intro/tutorial03/">1.2</a>
|
|---|
| 69 |
|
|---|
| 70 | </li>
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 | <li class="other">
|
|---|
| 75 |
|
|---|
| 76 | <a href="https://docs.djangoproject.com/en/1.3/intro/tutorial03/">1.3</a>
|
|---|
| 77 |
|
|---|
| 78 | </li>
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | <li class="other">
|
|---|
| 83 |
|
|---|
| 84 | <a href="https://docs.djangoproject.com/en/1.4/intro/tutorial03/">1.4</a>
|
|---|
| 85 |
|
|---|
| 86 | </li>
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | <li class="other">
|
|---|
| 91 |
|
|---|
| 92 | <a href="https://docs.djangoproject.com/en/1.5/intro/tutorial03/">1.5</a>
|
|---|
| 93 |
|
|---|
| 94 | </li>
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 | <li class="other">
|
|---|
| 99 |
|
|---|
| 100 | <a href="https://docs.djangoproject.com/en/1.6/intro/tutorial03/">1.6</a>
|
|---|
| 101 |
|
|---|
| 102 | </li>
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 | <li class="current dev" title="This document is for Django's development version, which can be significantly different from previous releases. Click on the links on the left to see other versions.">
|
|---|
| 108 | <span>Documentation version:
|
|---|
| 109 | <strong>
|
|---|
| 110 | development</strong>
|
|---|
| 111 | </span>
|
|---|
| 112 | </li>
|
|---|
| 113 | </ul>
|
|---|
| 114 | <div class="section" id="s-writing-your-first-django-app-part-3">
|
|---|
| 115 | <span id="writing-your-first-django-app-part-3"></span><h1>Writing your first Django app, part 3<a class="headerlink" href="#writing-your-first-django-app-part-3" title="Permalink to this headline">¶</a></h1>
|
|---|
| 116 | <p>This tutorial begins where <a class="reference internal" href="https://docs.djangoproject.com/en/dev/intro/tutorial02/"><em>Tutorial 2</em></a> left off. We’re
|
|---|
| 117 | continuing the Web-poll application and will focus on creating the public
|
|---|
| 118 | interface – “views.”</p>
|
|---|
| 119 | <div class="section" id="s-philosophy">
|
|---|
| 120 | <span id="philosophy"></span><h2>Philosophy<a class="headerlink" href="#philosophy" title="Permalink to this headline">¶</a></h2>
|
|---|
| 121 | <p>A view is a “type” of Web page in your Django application that generally serves
|
|---|
| 122 | a specific function and has a specific template. For example, in a blog
|
|---|
| 123 | application, you might have the following views:</p>
|
|---|
| 124 | <ul class="simple">
|
|---|
| 125 | <li>Blog homepage – displays the latest few entries.</li>
|
|---|
| 126 | <li>Entry “detail” page – permalink page for a single entry.</li>
|
|---|
| 127 | <li>Year-based archive page – displays all months with entries in the
|
|---|
| 128 | given year.</li>
|
|---|
| 129 | <li>Month-based archive page – displays all days with entries in the
|
|---|
| 130 | given month.</li>
|
|---|
| 131 | <li>Day-based archive page – displays all entries in the given day.</li>
|
|---|
| 132 | <li>Comment action – handles posting comments to a given entry.</li>
|
|---|
| 133 | </ul>
|
|---|
| 134 | <p>In our poll application, we’ll have the following four views:</p>
|
|---|
| 135 | <ul class="simple">
|
|---|
| 136 | <li>Question “index” page – displays the latest few questions.</li>
|
|---|
| 137 | <li>Question “detail” page – displays a question text, with no results but
|
|---|
| 138 | with a form to vote.</li>
|
|---|
| 139 | <li>Question “results” page – displays results for a particular question.</li>
|
|---|
| 140 | <li>Vote action – handles voting for a particular choice in a particular
|
|---|
| 141 | question.</li>
|
|---|
| 142 | </ul>
|
|---|
| 143 | <p>In Django, web pages and other content are delivered by views. Each view is
|
|---|
| 144 | represented by a simple Python function (or method, in the case of class-based
|
|---|
| 145 | views). Django will choose a view by examining the URL that’s requested (to be
|
|---|
| 146 | precise, the part of the URL after the domain name).</p>
|
|---|
| 147 | <p>Now in your time on the web you may have come across such beauties as
|
|---|
| 148 | “ME2/Sites/dirmod.asp?sid=&type=gen&mod=Core+Pages&gid=A6CD4967199A42D9B65B1B”.
|
|---|
| 149 | You will be pleased to know that Django allows us much more elegant
|
|---|
| 150 | <em>URL patterns</em> than that.</p>
|
|---|
| 151 | <p>A URL pattern is simply the general form of a URL - for example:
|
|---|
| 152 | <tt class="docutils literal"><span class="pre">/newsarchive/<year>/<month>/</span></tt>.</p>
|
|---|
| 153 | <p>To get from a URL to a view, Django uses what are known as ‘URLconfs’. A
|
|---|
| 154 | URLconf maps URL patterns (described as regular expressions) to views.</p>
|
|---|
| 155 | <p>This tutorial provides basic instruction in the use of URLconfs, and you can
|
|---|
| 156 | refer to <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/urlresolvers/#module-django.core.urlresolvers" title="django.core.urlresolvers"><tt class="xref py py-mod docutils literal"><span class="pre">django.core.urlresolvers</span></tt></a> for more information.</p>
|
|---|
| 157 | </div>
|
|---|
| 158 | <div class="section" id="s-write-your-first-view">
|
|---|
| 159 | <span id="write-your-first-view"></span><h2>Write your first view<a class="headerlink" href="#write-your-first-view" title="Permalink to this headline">¶</a></h2>
|
|---|
| 160 | <p>Let’s write the first view. Open the file <tt class="docutils literal"><span class="pre">polls/views.py</span></tt>
|
|---|
| 161 | and put the following Python code in it:</p>
|
|---|
| 162 | <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>
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 | <span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
|
|---|
| 166 | <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">"Hello, world. You're at the polls index."</span><span class="p">)</span>
|
|---|
| 167 | </pre></div>
|
|---|
| 168 | </div>
|
|---|
| 169 | <p>This is the simplest view possible in Django. To call the view, we need to map
|
|---|
| 170 | it to a URL - and for this we need a URLconf.</p>
|
|---|
| 171 | <p>To create a URLconf in the polls directory, create a file called <tt class="docutils literal"><span class="pre">urls.py</span></tt>.
|
|---|
| 172 | Your app directory should now look like:</p>
|
|---|
| 173 | <div class="highlight-python"><pre>polls/
|
|---|
| 174 | __init__.py
|
|---|
| 175 | admin.py
|
|---|
| 176 | models.py
|
|---|
| 177 | tests.py
|
|---|
| 178 | urls.py
|
|---|
| 179 | views.py</pre>
|
|---|
| 180 | </div>
|
|---|
| 181 | <p>In the <tt class="docutils literal"><span class="pre">polls/urls.py</span></tt> file include the following code:</p>
|
|---|
| 182 | <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>
|
|---|
| 183 |
|
|---|
| 184 | <span class="kn">from</span> <span class="nn">polls</span> <span class="kn">import</span> <span class="n">views</span>
|
|---|
| 185 |
|
|---|
| 186 | <span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
|
|---|
| 187 | <span class="n">url</span><span class="p">(</span><span class="s">r'^$'</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="s">'index'</span><span class="p">)</span>
|
|---|
| 188 | <span class="p">)</span>
|
|---|
| 189 | </pre></div>
|
|---|
| 190 | </div>
|
|---|
| 191 | <p>The next step is to point the root URLconf at the <tt class="docutils literal"><span class="pre">polls.urls</span></tt> module. In
|
|---|
| 192 | <tt class="docutils literal"><span class="pre">mysite/urls.py</span></tt> insert an <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/urls/#django.conf.urls.include" title="django.conf.urls.include"><tt class="xref py py-func docutils literal"><span class="pre">include()</span></tt></a>, leaving you
|
|---|
| 193 | with:</p>
|
|---|
| 194 | <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">include</span><span class="p">,</span> <span class="n">url</span>
|
|---|
| 195 |
|
|---|
| 196 | <span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
|
|---|
| 197 | <span class="n">admin</span><span class="o">.</span><span class="n">autodiscover</span><span class="p">()</span>
|
|---|
| 198 |
|
|---|
| 199 | <span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
|
|---|
| 200 | <span class="n">url</span><span class="p">(</span><span class="s">r'^polls/'</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">'polls.urls'</span><span class="p">)),</span>
|
|---|
| 201 | <span class="n">url</span><span class="p">(</span><span class="s">r'^admin/'</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>
|
|---|
| 202 | <span class="p">)</span>
|
|---|
| 203 | </pre></div>
|
|---|
| 204 | </div>
|
|---|
| 205 | <p>You have now wired an <tt class="docutils literal"><span class="pre">index</span></tt> view into the URLconf. Go to
|
|---|
| 206 | <a class="reference external" href="http://localhost:8000/polls/">http://localhost:8000/polls/</a> in your browser, and you should see the text
|
|---|
| 207 | “<em>Hello, world. You’re at the polls index.</em>”, which you defined in the
|
|---|
| 208 | <tt class="docutils literal"><span class="pre">index</span></tt> view.</p>
|
|---|
| 209 | <p>The <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/urls/#django.conf.urls.url" title="django.conf.urls.url"><tt class="xref py py-func docutils literal"><span class="pre">url()</span></tt></a> function is passed four arguments, two
|
|---|
| 210 | required: <tt class="docutils literal"><span class="pre">regex</span></tt> and <tt class="docutils literal"><span class="pre">view</span></tt>, and two optional: <tt class="docutils literal"><span class="pre">kwargs</span></tt>, and <tt class="docutils literal"><span class="pre">name</span></tt>.
|
|---|
| 211 | At this point, it’s worth reviewing what these arguments are for.</p>
|
|---|
| 212 | <div class="section" id="s-url-argument-regex">
|
|---|
| 213 | <span id="url-argument-regex"></span><h3><a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/urls/#django.conf.urls.url" title="django.conf.urls.url"><tt class="xref py py-func docutils literal"><span class="pre">url()</span></tt></a> argument: regex<a class="headerlink" href="#url-argument-regex" title="Permalink to this headline">¶</a></h3>
|
|---|
| 214 | <p>The term “regex” is a commonly used short form meaning “regular expression”,
|
|---|
| 215 | which is a syntax for matching patterns in strings, or in this case, url
|
|---|
| 216 | patterns. Django starts at the first regular expression and makes its way down
|
|---|
| 217 | the list, comparing the requested URL against each regular expression until it
|
|---|
| 218 | finds one that matches.</p>
|
|---|
| 219 | <p>Note that these regular expressions do not search GET and POST parameters, or
|
|---|
| 220 | the domain name. For example, in a request to
|
|---|
| 221 | <tt class="docutils literal"><span class="pre">http://www.example.com/myapp/</span></tt>, the URLconf will look for <tt class="docutils literal"><span class="pre">myapp/</span></tt>. In a
|
|---|
| 222 | request to <tt class="docutils literal"><span class="pre">http://www.example.com/myapp/?page=3</span></tt>, the URLconf will also
|
|---|
| 223 | look for <tt class="docutils literal"><span class="pre">myapp/</span></tt>.</p>
|
|---|
| 224 | <p>If you need help with regular expressions, see <a class="reference external" href="http://en.wikipedia.org/wiki/Regular_expression">Wikipedia’s entry</a> and the
|
|---|
| 225 | documentation of the <a class="reference external" href="http://docs.python.org/2.7/library/re.html#re" title="(in Python v2.7)"><tt class="xref py py-mod docutils literal"><span class="pre">re</span></tt></a> module. Also, the O’Reilly book “Mastering
|
|---|
| 226 | Regular Expressions” by Jeffrey Friedl is fantastic. In practice, however,
|
|---|
| 227 | you don’t need to be an expert on regular expressions, as you really only need
|
|---|
| 228 | to know how to capture simple patterns. In fact, complex regexes can have poor
|
|---|
| 229 | lookup performance, so you probably shouldn’t rely on the full power of regexes.</p>
|
|---|
| 230 | <p>Finally, a performance note: these regular expressions are compiled the first
|
|---|
| 231 | time the URLconf module is loaded. They’re super fast (as long as the lookups
|
|---|
| 232 | aren’t too complex as noted above).</p>
|
|---|
| 233 | </div>
|
|---|
| 234 | <div class="section" id="s-url-argument-view">
|
|---|
| 235 | <span id="url-argument-view"></span><h3><a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/urls/#django.conf.urls.url" title="django.conf.urls.url"><tt class="xref py py-func docutils literal"><span class="pre">url()</span></tt></a> argument: view<a class="headerlink" href="#url-argument-view" title="Permalink to this headline">¶</a></h3>
|
|---|
| 236 | <p>When Django finds a regular expression match, Django calls the specified view
|
|---|
| 237 | function, with an <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest" title="django.http.HttpRequest"><tt class="xref py py-class docutils literal"><span class="pre">HttpRequest</span></tt></a> object as the first
|
|---|
| 238 | argument and any “captured” values from the regular expression as other
|
|---|
| 239 | arguments. If the regex uses simple captures, values are passed as positional
|
|---|
| 240 | arguments; if it uses named captures, values are passed as keyword arguments.
|
|---|
| 241 | We’ll give an example of this in a bit.</p>
|
|---|
| 242 | </div>
|
|---|
| 243 | <div class="section" id="s-url-argument-kwargs">
|
|---|
| 244 | <span id="url-argument-kwargs"></span><h3><a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/urls/#django.conf.urls.url" title="django.conf.urls.url"><tt class="xref py py-func docutils literal"><span class="pre">url()</span></tt></a> argument: kwargs<a class="headerlink" href="#url-argument-kwargs" title="Permalink to this headline">¶</a></h3>
|
|---|
| 245 | <p>Arbitrary keyword arguments can be passed in a dictionary to the target view. We
|
|---|
| 246 | aren’t going to use this feature of Django in the tutorial.</p>
|
|---|
| 247 | </div>
|
|---|
| 248 | <div class="section" id="s-url-argument-name">
|
|---|
| 249 | <span id="url-argument-name"></span><h3><a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/urls/#django.conf.urls.url" title="django.conf.urls.url"><tt class="xref py py-func docutils literal"><span class="pre">url()</span></tt></a> argument: name<a class="headerlink" href="#url-argument-name" title="Permalink to this headline">¶</a></h3>
|
|---|
| 250 | <p>Naming your URL lets you refer to it unambiguously from elsewhere in Django
|
|---|
| 251 | especially templates. This powerful feature allows you to make global changes
|
|---|
| 252 | to the url patterns of your project while only touching a single file.</p>
|
|---|
| 253 | </div>
|
|---|
| 254 | </div>
|
|---|
| 255 | <div class="section" id="s-writing-more-views">
|
|---|
| 256 | <span id="writing-more-views"></span><h2>Writing more views<a class="headerlink" href="#writing-more-views" title="Permalink to this headline">¶</a></h2>
|
|---|
| 257 | <p>Now let’s add a few more views to <tt class="docutils literal"><span class="pre">polls/views.py</span></tt>. These views are
|
|---|
| 258 | slightly different, because they take an argument:</p>
|
|---|
| 259 | <div class="highlight-python"><div class="highlight"><pre><span class="k">def</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>
|
|---|
| 260 | <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">"You're looking at question </span><span class="si">%s</span><span class="s">."</span> <span class="o">%</span> <span class="n">question_id</span><span class="p">)</span>
|
|---|
| 261 |
|
|---|
| 262 | <span class="k">def</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>
|
|---|
| 263 | <span class="n">response</span> <span class="o">=</span> <span class="s">"You're looking at the results of question </span><span class="si">%s</span><span class="s">."</span>
|
|---|
| 264 | <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>
|
|---|
| 265 |
|
|---|
| 266 | <span class="k">def</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>
|
|---|
| 267 | <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">"You're voting on question </span><span class="si">%s</span><span class="s">."</span> <span class="o">%</span> <span class="n">question_id</span><span class="p">)</span>
|
|---|
| 268 | </pre></div>
|
|---|
| 269 | </div>
|
|---|
| 270 | <p>Wire these new views into the <tt class="docutils literal"><span class="pre">polls.urls</span></tt> module by adding the following
|
|---|
| 271 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/urls/#django.conf.urls.url" title="django.conf.urls.url"><tt class="xref py py-func docutils literal"><span class="pre">url()</span></tt></a> calls:</p>
|
|---|
| 272 | <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">url</span>
|
|---|
| 273 |
|
|---|
| 274 | <span class="kn">from</span> <span class="nn">polls</span> <span class="kn">import</span> <span class="n">views</span>
|
|---|
| 275 |
|
|---|
| 276 | <span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
|
|---|
| 277 | <span class="c"># ex: /polls/</span>
|
|---|
| 278 | <span class="n">url</span><span class="p">(</span><span class="s">r'^$'</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="s">'index'</span><span class="p">),</span>
|
|---|
| 279 | <span class="c"># ex: /polls/5/</span>
|
|---|
| 280 | <span class="n">url</span><span class="p">(</span><span class="s">r'^(?P<question_id>\d+)/$'</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="s">'detail'</span><span class="p">),</span>
|
|---|
| 281 | <span class="c"># ex: /polls/5/results/</span>
|
|---|
| 282 | <span class="n">url</span><span class="p">(</span><span class="s">r'^(?P<question_id>\d+)/results/$'</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="s">'results'</span><span class="p">),</span>
|
|---|
| 283 | <span class="c"># ex: /polls/5/vote/</span>
|
|---|
| 284 | <span class="n">url</span><span class="p">(</span><span class="s">r'^(?P<question_id>\d+)/vote/$'</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="s">'vote'</span><span class="p">),</span>
|
|---|
| 285 | <span class="p">)</span>
|
|---|
| 286 | </pre></div>
|
|---|
| 287 | </div>
|
|---|
| 288 | <p>Take a look in your browser, at “/polls/34/”. It’ll run the <tt class="docutils literal"><span class="pre">detail()</span></tt>
|
|---|
| 289 | method and display whatever ID you provide in the URL. Try
|
|---|
| 290 | “/polls/34/results/” and “/polls/34/vote/” too – these will display the
|
|---|
| 291 | placeholder results and voting pages.</p>
|
|---|
| 292 | <p>When somebody requests a page from your Web site – say, “/polls/34/”, Django
|
|---|
| 293 | will load the <tt class="docutils literal"><span class="pre">mysite.urls</span></tt> Python module because it’s pointed to by the
|
|---|
| 294 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-ROOT_URLCONF"><tt class="xref std std-setting docutils literal"><span class="pre">ROOT_URLCONF</span></tt></a> setting. It finds the variable named <tt class="docutils literal"><span class="pre">urlpatterns</span></tt>
|
|---|
| 295 | and traverses the regular expressions in order. The
|
|---|
| 296 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/urls/#django.conf.urls.include" title="django.conf.urls.include"><tt class="xref py py-func docutils literal"><span class="pre">include()</span></tt></a> functions we are using simply reference
|
|---|
| 297 | other URLconfs. Note that the regular expressions for the
|
|---|
| 298 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/urls/#django.conf.urls.include" title="django.conf.urls.include"><tt class="xref py py-func docutils literal"><span class="pre">include()</span></tt></a> functions don’t have a <tt class="docutils literal"><span class="pre">$</span></tt> (end-of-string
|
|---|
| 299 | match character) but rather a trailing slash. Whenever Django encounters
|
|---|
| 300 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/urls/#django.conf.urls.include" title="django.conf.urls.include"><tt class="xref py py-func docutils literal"><span class="pre">include()</span></tt></a>, it chops off whatever part of the URL
|
|---|
| 301 | matched up to that point and sends the remaining string to the included
|
|---|
| 302 | URLconf for further processing.</p>
|
|---|
| 303 | <p>The idea behind <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/urls/#django.conf.urls.include" title="django.conf.urls.include"><tt class="xref py py-func docutils literal"><span class="pre">include()</span></tt></a> is to make it easy to
|
|---|
| 304 | plug-and-play URLs. Since polls are in their own URLconf
|
|---|
| 305 | (<tt class="docutils literal"><span class="pre">polls/urls.py</span></tt>), they can be placed under “/polls/”, or under
|
|---|
| 306 | “/fun_polls/”, or under “/content/polls/”, or any other path root, and the
|
|---|
| 307 | app will still work.</p>
|
|---|
| 308 | <p>Here’s what happens if a user goes to “/polls/34/” in this system:</p>
|
|---|
| 309 | <ul>
|
|---|
| 310 | <li><p class="first">Django will find the match at <tt class="docutils literal"><span class="pre">'^polls/'</span></tt></p>
|
|---|
| 311 | </li>
|
|---|
| 312 | <li><p class="first">Then, Django will strip off the matching text (<tt class="docutils literal"><span class="pre">"polls/"</span></tt>) and send the
|
|---|
| 313 | remaining text – <tt class="docutils literal"><span class="pre">"34/"</span></tt> – to the ‘polls.urls’ URLconf for
|
|---|
| 314 | further processing which matches <tt class="docutils literal"><span class="pre">r'^(?P<question_id>\d+)/$'</span></tt> resulting in a
|
|---|
| 315 | call to the <tt class="docutils literal"><span class="pre">detail()</span></tt> view like so:</p>
|
|---|
| 316 | <div class="highlight-python"><pre>detail(request=<HttpRequest object>, question_id='34')</pre>
|
|---|
| 317 | </div>
|
|---|
| 318 | </li>
|
|---|
| 319 | </ul>
|
|---|
| 320 | <p>The <tt class="docutils literal"><span class="pre">question_id='34'</span></tt> part comes from <tt class="docutils literal"><span class="pre">(?P<question_id>\d+)</span></tt>. Using parentheses
|
|---|
| 321 | around a pattern “captures” the text matched by that pattern and sends it as an
|
|---|
| 322 | argument to the view function; <tt class="docutils literal"><span class="pre">?P<question_id></span></tt> defines the name that will
|
|---|
| 323 | be used to identify the matched pattern; and <tt class="docutils literal"><span class="pre">\d+</span></tt> is a regular expression to
|
|---|
| 324 | match a sequence of digits (i.e., a number).</p>
|
|---|
| 325 | <p>Because the URL patterns are regular expressions, there really is no limit on
|
|---|
| 326 | what you can do with them. And there’s no need to add URL cruft such as
|
|---|
| 327 | <tt class="docutils literal"><span class="pre">.html</span></tt> – unless you want to, in which case you can do something like
|
|---|
| 328 | this:</p>
|
|---|
| 329 | <div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="s">r'^polls/latest\.html$'</span><span class="p">,</span> <span class="s">'polls.views.index'</span><span class="p">),</span>
|
|---|
| 330 | </pre></div>
|
|---|
| 331 | </div>
|
|---|
| 332 | <p>But, don’t do that. It’s silly.</p>
|
|---|
| 333 | </div>
|
|---|
| 334 | <div class="section" id="s-write-views-that-actually-do-something">
|
|---|
| 335 | <span id="write-views-that-actually-do-something"></span><h2>Write views that actually do something<a class="headerlink" href="#write-views-that-actually-do-something" title="Permalink to this headline">¶</a></h2>
|
|---|
| 336 | <p>Each view is responsible for doing one of two things: returning an
|
|---|
| 337 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponse" title="django.http.HttpResponse"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></tt></a> object containing the content for the
|
|---|
| 338 | requested page, or raising an exception such as <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/views/#django.http.Http404" title="django.http.Http404"><tt class="xref py py-exc docutils literal"><span class="pre">Http404</span></tt></a>. The
|
|---|
| 339 | rest is up to you.</p>
|
|---|
| 340 | <p>Your view can read records from a database, or not. It can use a template
|
|---|
| 341 | system such as Django’s – or a third-party Python template system – or not.
|
|---|
| 342 | It can generate a PDF file, output XML, create a ZIP file on the fly, anything
|
|---|
| 343 | you want, using whatever Python libraries you want.</p>
|
|---|
| 344 | <p>All Django wants is that <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponse" title="django.http.HttpResponse"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></tt></a>. Or an exception.</p>
|
|---|
| 345 | <p>Because it’s convenient, let’s use Django’s own database API, which we covered
|
|---|
| 346 | in <a class="reference internal" href="https://docs.djangoproject.com/en/dev/intro/tutorial01/"><em>Tutorial 1</em></a>. Here’s one stab at the <tt class="docutils literal"><span class="pre">index()</span></tt>
|
|---|
| 347 | view, which displays the latest 5 poll questions in the system, separated by
|
|---|
| 348 | commas, according to publication date:</p>
|
|---|
| 349 | <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>
|
|---|
| 350 |
|
|---|
| 351 | <span class="kn">from</span> <span class="nn">polls.models</span> <span class="kn">import</span> <span class="n">Question</span>
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 | <span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
|
|---|
| 355 | <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="s">'-pub_date'</span><span class="p">)[:</span><span class="mi">5</span><span class="p">]</span>
|
|---|
| 356 | <span class="n">output</span> <span class="o">=</span> <span class="s">', '</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>
|
|---|
| 357 | <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
|
|---|
| 358 | </pre></div>
|
|---|
| 359 | </div>
|
|---|
| 360 | <p>There’s a problem here, though: the page’s design is hard-coded in the view. If
|
|---|
| 361 | you want to change the way the page looks, you’ll have to edit this Python code.
|
|---|
| 362 | So let’s use Django’s template system to separate the design from Python by
|
|---|
| 363 | creating a template that the view can use.</p>
|
|---|
| 364 | <p>First, create a directory called <tt class="docutils literal"><span class="pre">templates</span></tt> in your <tt class="docutils literal"><span class="pre">polls</span></tt> directory.
|
|---|
| 365 | Django will look for templates in there.</p>
|
|---|
| 366 | <p>Django’s <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATE_LOADERS"><tt class="xref std std-setting docutils literal"><span class="pre">TEMPLATE_LOADERS</span></tt></a> setting contains a list of callables that
|
|---|
| 367 | know how to import templates from various sources. One of the defaults is
|
|---|
| 368 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/templates/api/#django.template.loaders.app_directories.Loader" title="django.template.loaders.app_directories.Loader"><tt class="xref py py-class docutils literal"><span class="pre">django.template.loaders.app_directories.Loader</span></tt></a> which looks for a
|
|---|
| 369 | “templates” subdirectory in each of the <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-INSTALLED_APPS"><tt class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></tt></a> - this is how
|
|---|
| 370 | Django knows to find the polls templates even though we didn’t modify
|
|---|
| 371 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATE_DIRS"><tt class="xref std std-setting docutils literal"><span class="pre">TEMPLATE_DIRS</span></tt></a>, as we did in <a class="reference internal" href="https://docs.djangoproject.com/en/dev/intro/tutorial02/#ref-customizing-your-projects-templates"><em>Tutorial 2</em></a>.</p>
|
|---|
| 372 | <div class="admonition-organizing-templates admonition">
|
|---|
| 373 | <p class="first admonition-title">Organizing templates</p>
|
|---|
| 374 | <p class="last">We <em>could</em> have all our templates together, in one big templates directory,
|
|---|
| 375 | and it would work perfectly well. However, this template belongs to the
|
|---|
| 376 | polls application, so unlike the admin template we created in the previous
|
|---|
| 377 | tutorial, we’ll put this one in the application’s template directory
|
|---|
| 378 | (<tt class="docutils literal"><span class="pre">polls/templates</span></tt>) rather than the project’s (<tt class="docutils literal"><span class="pre">templates</span></tt>). We’ll
|
|---|
| 379 | discuss in more detail in the <a class="reference internal" href="https://docs.djangoproject.com/en/dev/intro/reusable-apps/"><em>reusable apps tutorial</em></a> <em>why</em> we do this.</p>
|
|---|
| 380 | </div>
|
|---|
| 381 | <p>Within the <tt class="docutils literal"><span class="pre">templates</span></tt> directory you have just created, create another
|
|---|
| 382 | directory called <tt class="docutils literal"><span class="pre">polls</span></tt>, and within that create a file called
|
|---|
| 383 | <tt class="docutils literal"><span class="pre">index.html</span></tt>. In other words, your template should be at
|
|---|
| 384 | <tt class="docutils literal"><span class="pre">polls/templates/polls/index.html</span></tt>. Because of how the <tt class="docutils literal"><span class="pre">app_directories</span></tt>
|
|---|
| 385 | template loader works as described above, you can refer to this template within
|
|---|
| 386 | Django simply as <tt class="docutils literal"><span class="pre">polls/index.html</span></tt>.</p>
|
|---|
| 387 | <div class="admonition-template-namespacing admonition">
|
|---|
| 388 | <p class="first admonition-title">Template namespacing</p>
|
|---|
| 389 | <p class="last">Now we <em>might</em> be able to get away with putting our templates directly in
|
|---|
| 390 | <tt class="docutils literal"><span class="pre">polls/templates</span></tt> (rather than creating another <tt class="docutils literal"><span class="pre">polls</span></tt> subdirectory),
|
|---|
| 391 | but it would actually be a bad idea. Django will choose the first template
|
|---|
| 392 | it finds whose name matches, and if you had a template with the same name
|
|---|
| 393 | in a <em>different</em> application, Django would be unable to distinguish between
|
|---|
| 394 | them. We need to be able to point Django at the right one, and the easiest
|
|---|
| 395 | way to ensure this is by <em>namespacing</em> them. That is, by putting those
|
|---|
| 396 | templates inside <em>another</em> directory named for the application itself.</p>
|
|---|
| 397 | </div>
|
|---|
| 398 | <p>Put the following code in that template:</p>
|
|---|
| 399 | <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">latest_question_list</span> <span class="cp">%}</span>
|
|---|
| 400 | <span class="nt"><ul></span>
|
|---|
| 401 | <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>
|
|---|
| 402 | <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"/polls/</span><span class="cp">{{</span> <span class="nv">question.id</span> <span class="cp">}}</span><span class="s">/"</span><span class="nt">></span><span class="cp">{{</span> <span class="nv">question.question_text</span> <span class="cp">}}</span><span class="nt"></a></li></span>
|
|---|
| 403 | <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
|
|---|
| 404 | <span class="nt"></ul></span>
|
|---|
| 405 | <span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span>
|
|---|
| 406 | <span class="nt"><p></span>No polls are available.<span class="nt"></p></span>
|
|---|
| 407 | <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
|
|---|
| 408 | </pre></div>
|
|---|
| 409 | </div>
|
|---|
| 410 | <p>Now let’s update our <tt class="docutils literal"><span class="pre">index</span></tt> view in <tt class="docutils literal"><span class="pre">polls/views.py</span></tt> to use the template:</p>
|
|---|
| 411 | <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>
|
|---|
| 412 | <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>
|
|---|
| 413 |
|
|---|
| 414 | <span class="kn">from</span> <span class="nn">polls.models</span> <span class="kn">import</span> <span class="n">Question</span>
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 | <span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
|
|---|
| 418 | <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="s">'-pub_date'</span><span class="p">)[:</span><span class="mi">5</span><span class="p">]</span>
|
|---|
| 419 | <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="s">'polls/index.html'</span><span class="p">)</span>
|
|---|
| 420 | <span class="n">context</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>
|
|---|
| 421 | <span class="s">'latest_question_list'</span><span class="p">:</span> <span class="n">latest_question_list</span><span class="p">,</span>
|
|---|
| 422 | <span class="p">})</span>
|
|---|
| 423 | <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>
|
|---|
| 424 | </pre></div>
|
|---|
| 425 | </div>
|
|---|
| 426 | <p>That code loads the template called <tt class="docutils literal"><span class="pre">polls/index.html</span></tt> and passes it a
|
|---|
| 427 | context. The context is a dictionary mapping template variable names to Python
|
|---|
| 428 | objects.</p>
|
|---|
| 429 | <p>Load the page by pointing your browser at “/polls/”, and you should see a
|
|---|
| 430 | bulleted-list containing the “What’s up” question from Tutorial 1. The link points
|
|---|
| 431 | to the question’s detail page.</p>
|
|---|
| 432 | <div class="section" id="s-a-shortcut-render">
|
|---|
| 433 | <span id="a-shortcut-render"></span><h3>A shortcut: <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.render" title="django.shortcuts.render"><tt class="xref py py-func docutils literal"><span class="pre">render()</span></tt></a><a class="headerlink" href="#a-shortcut-render" title="Permalink to this headline">¶</a></h3>
|
|---|
| 434 | <p>It’s a very common idiom to load a template, fill a context and return an
|
|---|
| 435 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponse" title="django.http.HttpResponse"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></tt></a> object with the result of the rendered
|
|---|
| 436 | template. Django provides a shortcut. Here’s the full <tt class="docutils literal"><span class="pre">index()</span></tt> view,
|
|---|
| 437 | rewritten:</p>
|
|---|
| 438 | <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>
|
|---|
| 439 |
|
|---|
| 440 | <span class="kn">from</span> <span class="nn">polls.models</span> <span class="kn">import</span> <span class="n">Question</span>
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 | <span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
|
|---|
| 444 | <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">all</span><span class="p">()</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="s">'-pub_date'</span><span class="p">)[:</span><span class="mi">5</span><span class="p">]</span>
|
|---|
| 445 | <span class="n">context</span> <span class="o">=</span> <span class="p">{</span><span class="s">'latest_question_list'</span><span class="p">:</span> <span class="n">latest_question_list</span><span class="p">}</span>
|
|---|
| 446 | <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">'polls/index.html'</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>
|
|---|
| 447 | </pre></div>
|
|---|
| 448 | </div>
|
|---|
| 449 | <p>Note that once we’ve done this in all these views, we no longer need to import
|
|---|
| 450 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/templates/api/#module-django.template.loader" title="django.template.loader"><tt class="xref py py-mod docutils literal"><span class="pre">loader</span></tt></a>, <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/templates/api/#django.template.RequestContext" title="django.template.RequestContext"><tt class="xref py py-class docutils literal"><span class="pre">RequestContext</span></tt></a> and
|
|---|
| 451 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponse" title="django.http.HttpResponse"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></tt></a> (you’ll want to keep <tt class="docutils literal"><span class="pre">HttpResponse</span></tt> if you
|
|---|
| 452 | still have the stub methods for <tt class="docutils literal"><span class="pre">detail</span></tt>, <tt class="docutils literal"><span class="pre">results</span></tt>, and <tt class="docutils literal"><span class="pre">vote</span></tt>).</p>
|
|---|
| 453 | <p>The <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.render" title="django.shortcuts.render"><tt class="xref py py-func docutils literal"><span class="pre">render()</span></tt></a> function takes the request object as its
|
|---|
| 454 | first argument, a template name as its second argument and a dictionary as its
|
|---|
| 455 | optional third argument. It returns an <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponse" title="django.http.HttpResponse"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></tt></a>
|
|---|
| 456 | object of the given template rendered with the given context.</p>
|
|---|
| 457 | </div>
|
|---|
| 458 | </div>
|
|---|
| 459 | <div class="section" id="s-raising-a-404-error">
|
|---|
| 460 | <span id="raising-a-404-error"></span><h2>Raising a 404 error<a class="headerlink" href="#raising-a-404-error" title="Permalink to this headline">¶</a></h2>
|
|---|
| 461 | <p>Now, let’s tackle the question detail view – the page that displays the question text
|
|---|
| 462 | for a given poll. Here’s the view:</p>
|
|---|
| 463 | <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>
|
|---|
| 464 | <span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render</span>
|
|---|
| 465 |
|
|---|
| 466 | <span class="kn">from</span> <span class="nn">polls.models</span> <span class="kn">import</span> <span class="n">Question</span>
|
|---|
| 467 | <span class="c"># ...</span>
|
|---|
| 468 | <span class="k">def</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>
|
|---|
| 469 | <span class="k">try</span><span class="p">:</span>
|
|---|
| 470 | <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>
|
|---|
| 471 | <span class="k">except</span> <span class="n">Question</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">:</span>
|
|---|
| 472 | <span class="k">raise</span> <span class="n">Http404</span>
|
|---|
| 473 | <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">'polls/detail.html'</span><span class="p">,</span> <span class="p">{</span><span class="s">'question'</span><span class="p">:</span> <span class="n">question</span><span class="p">})</span>
|
|---|
| 474 | </pre></div>
|
|---|
| 475 | </div>
|
|---|
| 476 | <p>The new concept here: The view raises the <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/views/#django.http.Http404" title="django.http.Http404"><tt class="xref py py-exc docutils literal"><span class="pre">Http404</span></tt></a> exception
|
|---|
| 477 | if a question with the requested ID doesn’t exist.</p>
|
|---|
| 478 | <p>We’ll discuss what you could put in that <tt class="docutils literal"><span class="pre">polls/detail.html</span></tt> template a bit
|
|---|
| 479 | later, but if you’d like to quickly get the above example working, a file
|
|---|
| 480 | containing just:</p>
|
|---|
| 481 | <div class="highlight-python"><pre>{{ question }}</pre>
|
|---|
| 482 | </div>
|
|---|
| 483 | <p>will get you started for now.</p>
|
|---|
| 484 | <div class="section" id="s-a-shortcut-get-object-or-404">
|
|---|
| 485 | <span id="a-shortcut-get-object-or-404"></span><h3>A shortcut: <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.get_object_or_404" title="django.shortcuts.get_object_or_404"><tt class="xref py py-func docutils literal"><span class="pre">get_object_or_404()</span></tt></a><a class="headerlink" href="#a-shortcut-get-object-or-404" title="Permalink to this headline">¶</a></h3>
|
|---|
| 486 | <p>It’s a very common idiom to use <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#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>
|
|---|
| 487 | and raise <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/views/#django.http.Http404" title="django.http.Http404"><tt class="xref py py-exc docutils literal"><span class="pre">Http404</span></tt></a> if the object doesn’t exist. Django
|
|---|
| 488 | provides a shortcut. Here’s the <tt class="docutils literal"><span class="pre">detail()</span></tt> view, rewritten:</p>
|
|---|
| 489 | <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="p">,</span> <span class="n">get_object_or_404</span>
|
|---|
| 490 |
|
|---|
| 491 | <span class="kn">from</span> <span class="nn">polls.models</span> <span class="kn">import</span> <span class="n">Question</span>
|
|---|
| 492 | <span class="c"># ...</span>
|
|---|
| 493 | <span class="k">def</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>
|
|---|
| 494 | <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>
|
|---|
| 495 | <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">'polls/detail.html'</span><span class="p">,</span> <span class="p">{</span><span class="s">'question'</span><span class="p">:</span> <span class="n">question</span><span class="p">})</span>
|
|---|
| 496 | </pre></div>
|
|---|
| 497 | </div>
|
|---|
| 498 | <p>The <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.get_object_or_404" title="django.shortcuts.get_object_or_404"><tt class="xref py py-func docutils literal"><span class="pre">get_object_or_404()</span></tt></a> function takes a Django model
|
|---|
| 499 | as its first argument and an arbitrary number of keyword arguments, which it
|
|---|
| 500 | passes to the <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#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> function of the
|
|---|
| 501 | model’s manager. It raises <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/views/#django.http.Http404" title="django.http.Http404"><tt class="xref py py-exc docutils literal"><span class="pre">Http404</span></tt></a> if the object doesn’t
|
|---|
| 502 | exist.</p>
|
|---|
| 503 | <div class="admonition-philosophy admonition">
|
|---|
| 504 | <p class="first admonition-title">Philosophy</p>
|
|---|
| 505 | <p>Why do we use a helper function <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.get_object_or_404" title="django.shortcuts.get_object_or_404"><tt class="xref py py-func docutils literal"><span class="pre">get_object_or_404()</span></tt></a>
|
|---|
| 506 | instead of automatically catching the
|
|---|
| 507 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/exceptions/#django.core.exceptions.ObjectDoesNotExist" title="django.core.exceptions.ObjectDoesNotExist"><tt class="xref py py-exc docutils literal"><span class="pre">ObjectDoesNotExist</span></tt></a> exceptions at a higher
|
|---|
| 508 | level, or having the model API raise <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/views/#django.http.Http404" title="django.http.Http404"><tt class="xref py py-exc docutils literal"><span class="pre">Http404</span></tt></a> instead of
|
|---|
| 509 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/exceptions/#django.core.exceptions.ObjectDoesNotExist" title="django.core.exceptions.ObjectDoesNotExist"><tt class="xref py py-exc docutils literal"><span class="pre">ObjectDoesNotExist</span></tt></a>?</p>
|
|---|
| 510 | <p class="last">Because that would couple the model layer to the view layer. One of the
|
|---|
| 511 | foremost design goals of Django is to maintain loose coupling. Some
|
|---|
| 512 | controlled coupling is introduced in the <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#module-django.shortcuts" title="django.shortcuts: Convenience shortcuts that span multiple levels of Django's MVC stack."><tt class="xref py py-mod docutils literal"><span class="pre">django.shortcuts</span></tt></a> module.</p>
|
|---|
| 513 | </div>
|
|---|
| 514 | <p>There’s also a <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.get_list_or_404" title="django.shortcuts.get_list_or_404"><tt class="xref py py-func docutils literal"><span class="pre">get_list_or_404()</span></tt></a> function, which works
|
|---|
| 515 | just as <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.get_object_or_404" title="django.shortcuts.get_object_or_404"><tt class="xref py py-func docutils literal"><span class="pre">get_object_or_404()</span></tt></a> – except using
|
|---|
| 516 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#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> instead of
|
|---|
| 517 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#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>. It raises
|
|---|
| 518 | <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/http/views/#django.http.Http404" title="django.http.Http404"><tt class="xref py py-exc docutils literal"><span class="pre">Http404</span></tt></a> if the list is empty.</p>
|
|---|
| 519 | </div>
|
|---|
| 520 | </div>
|
|---|
| 521 | <div class="section" id="s-use-the-template-system">
|
|---|
| 522 | <span id="use-the-template-system"></span><h2>Use the template system<a class="headerlink" href="#use-the-template-system" title="Permalink to this headline">¶</a></h2>
|
|---|
| 523 | <p>Back to the <tt class="docutils literal"><span class="pre">detail()</span></tt> view for our poll application. Given the context
|
|---|
| 524 | variable <tt class="docutils literal"><span class="pre">question</span></tt>, here’s what the <tt class="docutils literal"><span class="pre">polls/detail.html</span></tt> template might look
|
|---|
| 525 | like:</p>
|
|---|
| 526 | <div class="highlight-html+django"><div class="highlight"><pre><span class="nt"><h1></span><span class="cp">{{</span> <span class="nv">question.question_text</span> <span class="cp">}}</span><span class="nt"></h1></span>
|
|---|
| 527 | <span class="nt"><ul></span>
|
|---|
| 528 | <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>
|
|---|
| 529 | <span class="nt"><li></span><span class="cp">{{</span> <span class="nv">choice.choice_text</span> <span class="cp">}}</span><span class="nt"></li></span>
|
|---|
| 530 | <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
|
|---|
| 531 | <span class="nt"></ul></span>
|
|---|
| 532 | </pre></div>
|
|---|
| 533 | </div>
|
|---|
| 534 | <p>The template system uses dot-lookup syntax to access variable attributes. In
|
|---|
| 535 | the example of <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">question.question_text</span> <span class="pre">}}</span></tt>, first Django does a dictionary lookup
|
|---|
| 536 | on the object <tt class="docutils literal"><span class="pre">question</span></tt>. Failing that, it tries an attribute lookup – which
|
|---|
| 537 | works, in this case. If attribute lookup had failed, it would’ve tried a
|
|---|
| 538 | list-index lookup.</p>
|
|---|
| 539 | <p>Method-calling happens in the <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/templates/builtins/#std:templatetag-for"><tt class="xref std std-ttag docutils literal"><span class="pre">{%</span> <span class="pre">for</span> <span class="pre">%}</span></tt></a> loop:
|
|---|
| 540 | <tt class="docutils literal"><span class="pre">question.choice_set.all</span></tt> is interpreted as the Python code
|
|---|
| 541 | <tt class="docutils literal"><span class="pre">question.choice_set.all()</span></tt>, which returns an iterable of <tt class="docutils literal"><span class="pre">Choice</span></tt> objects and is
|
|---|
| 542 | suitable for use in the <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/templates/builtins/#std:templatetag-for"><tt class="xref std std-ttag docutils literal"><span class="pre">{%</span> <span class="pre">for</span> <span class="pre">%}</span></tt></a> tag.</p>
|
|---|
| 543 | <p>See the <a class="reference internal" href="https://docs.djangoproject.com/en/dev/topics/templates/"><em>template guide</em></a> for more about templates.</p>
|
|---|
| 544 | </div>
|
|---|
| 545 | <div class="section" id="s-removing-hardcoded-urls-in-templates">
|
|---|
| 546 | <span id="removing-hardcoded-urls-in-templates"></span><h2>Removing hardcoded URLs in templates<a class="headerlink" href="#removing-hardcoded-urls-in-templates" title="Permalink to this headline">¶</a></h2>
|
|---|
| 547 | <p>Remember, when we wrote the link to a question in the <tt class="docutils literal"><span class="pre">polls/index.html</span></tt>
|
|---|
| 548 | template, the link was partially hardcoded like this:</p>
|
|---|
| 549 | <div class="highlight-html+django"><div class="highlight"><pre><span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"/polls/</span><span class="cp">{{</span> <span class="nv">question.id</span> <span class="cp">}}</span><span class="s">/"</span><span class="nt">></span><span class="cp">{{</span> <span class="nv">question.question_text</span> <span class="cp">}}</span><span class="nt"></a></li></span>
|
|---|
| 550 | </pre></div>
|
|---|
| 551 | </div>
|
|---|
| 552 | <p>The problem with this hardcoded, tightly-coupled approach is that it becomes
|
|---|
| 553 | challenging to change URLs on projects with a lot of templates. However, since
|
|---|
| 554 | you defined the name argument in the <a class="reference internal" href="https://docs.djangoproject.com/en/dev/ref/urls/#django.conf.urls.url" title="django.conf.urls.url"><tt class="xref py py-func docutils literal"><span class="pre">url()</span></tt></a> functions in
|
|---|
| 555 | the <tt class="docutils literal"><span class="pre">polls.urls</span></tt> module, you can remove a reliance on specific URL paths
|
|---|
| 556 | defined in your url configurations by using the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">%}</span></tt> template tag:</p>
|
|---|
| 557 | <div class="highlight-html+django"><div class="highlight"><pre><span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">'detail'</span> <span class="nv">question.id</span> <span class="cp">%}</span><span class="s">"</span><span class="nt">></span><span class="cp">{{</span> <span class="nv">question.question_text</span> <span class="cp">}}</span><span class="nt"></a></li></span>
|
|---|
| 558 | </pre></div>
|
|---|
| 559 | </div>
|
|---|
| 560 | <div class="admonition note">
|
|---|
| 561 | <p class="first admonition-title">Note</p>
|
|---|
| 562 | <p>If <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">'detail'</span> <span class="pre">question.id</span> <span class="pre">%}</span></tt> (with quotes) doesn’t work, but
|
|---|
| 563 | <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">detail</span> <span class="pre">question.id</span> <span class="pre">%}</span></tt> (without quotes) does, that means you’re
|
|---|
| 564 | using a version of Django < 1.5. In this case, add the following
|
|---|
| 565 | declaration at the top of your template:</p>
|
|---|
| 566 | <div class="last highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">load</span> <span class="nv">url</span> <span class="nv">from</span> <span class="nv">future</span> <span class="cp">%}</span>
|
|---|
| 567 | </pre></div>
|
|---|
| 568 | </div>
|
|---|
| 569 | </div>
|
|---|
| 570 | <p>The way this works is by looking up the URL definition as specified in the
|
|---|
| 571 | <tt class="docutils literal"><span class="pre">polls.urls</span></tt> module. You can see exactly where the URL name of ‘detail’ is
|
|---|
| 572 | defined below:</p>
|
|---|
| 573 | <div class="highlight-python"><div class="highlight"><pre><span class="o">...</span>
|
|---|
| 574 | <span class="c"># the 'name' value as called by the {% url %} template tag</span>
|
|---|
| 575 | <span class="n">url</span><span class="p">(</span><span class="s">r'^(?P<question_id>\d+)/$'</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="s">'detail'</span><span class="p">),</span>
|
|---|
| 576 | <span class="o">...</span>
|
|---|
| 577 | </pre></div>
|
|---|
| 578 | </div>
|
|---|
| 579 | <p>If you want to change the URL of the polls detail view to something else,
|
|---|
| 580 | perhaps to something like <tt class="docutils literal"><span class="pre">polls/specifics/12/</span></tt> instead of doing it in the
|
|---|
| 581 | template (or templates) you would change it in <tt class="docutils literal"><span class="pre">polls/urls.py</span></tt>:</p>
|
|---|
| 582 | <div class="highlight-python"><div class="highlight"><pre><span class="o">...</span>
|
|---|
| 583 | <span class="c"># added the word 'specifics'</span>
|
|---|
| 584 | <span class="n">url</span><span class="p">(</span><span class="s">r'^specifics/(?P<question_id>\d+)/$'</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="s">'detail'</span><span class="p">),</span>
|
|---|
| 585 | <span class="o">...</span>
|
|---|
| 586 | </pre></div>
|
|---|
| 587 | </div>
|
|---|
| 588 | </div>
|
|---|
| 589 | <div class="section" id="s-namespacing-url-names">
|
|---|
| 590 | <span id="namespacing-url-names"></span><h2>Namespacing URL names<a class="headerlink" href="#namespacing-url-names" title="Permalink to this headline">¶</a></h2>
|
|---|
| 591 | <p>The tutorial project has just one app, <tt class="docutils literal"><span class="pre">polls</span></tt>. In real Django projects,
|
|---|
| 592 | there might be five, ten, twenty apps or more. How does Django differentiate
|
|---|
| 593 | the URL names between them? For example, the <tt class="docutils literal"><span class="pre">polls</span></tt> app has a <tt class="docutils literal"><span class="pre">detail</span></tt>
|
|---|
| 594 | view, and so might an app on the same project that is for a blog. How does one
|
|---|
| 595 | make it so that Django knows which app view to create for a url when using the
|
|---|
| 596 | <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">%}</span></tt> template tag?</p>
|
|---|
| 597 | <p>The answer is to add namespaces to your root URLconf. In the <tt class="docutils literal"><span class="pre">mysite/urls.py</span></tt>
|
|---|
| 598 | file (the project’s <tt class="docutils literal"><span class="pre">urls.py</span></tt>, not the application’s), go ahead and change
|
|---|
| 599 | it to include namespacing:</p>
|
|---|
| 600 | <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">include</span><span class="p">,</span> <span class="n">url</span>
|
|---|
| 601 |
|
|---|
| 602 | <span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
|
|---|
| 603 | <span class="n">admin</span><span class="o">.</span><span class="n">autodiscover</span><span class="p">()</span>
|
|---|
| 604 |
|
|---|
| 605 | <span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
|
|---|
| 606 | <span class="n">url</span><span class="p">(</span><span class="s">r'^polls/'</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">'polls.urls'</span><span class="p">,</span> <span class="n">namespace</span><span class="o">=</span><span class="s">"polls"</span><span class="p">)),</span>
|
|---|
| 607 | <span class="n">url</span><span class="p">(</span><span class="s">r'^admin/'</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>
|
|---|
| 608 | <span class="p">)</span>
|
|---|
| 609 | </pre></div>
|
|---|
| 610 | </div>
|
|---|
| 611 | <p>Now change your <tt class="docutils literal"><span class="pre">polls/index.html</span></tt> template from:</p>
|
|---|
| 612 | <div class="highlight-html+django"><div class="highlight"><pre><span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">'detail'</span> <span class="nv">question.id</span> <span class="cp">%}</span><span class="s">"</span><span class="nt">></span><span class="cp">{{</span> <span class="nv">question.question_text</span> <span class="cp">}}</span><span class="nt"></a></li></span>
|
|---|
| 613 | </pre></div>
|
|---|
| 614 | </div>
|
|---|
| 615 | <p>to point at the namespaced detail view:</p>
|
|---|
| 616 | <div class="highlight-html+django"><div class="highlight"><pre><span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">'polls:detail'</span> <span class="nv">question.id</span> <span class="cp">%}</span><span class="s">"</span><span class="nt">></span><span class="cp">{{</span> <span class="nv">question.question_text</span> <span class="cp">}}</span><span class="nt"></a></li></span>
|
|---|
| 617 | </pre></div>
|
|---|
| 618 | </div>
|
|---|
| 619 | <p>When you’re comfortable with writing views, read <a class="reference internal" href="https://docs.djangoproject.com/en/dev/intro/tutorial04/"><em>part 4 of this tutorial</em></a> to learn about simple form processing and generic views.</p>
|
|---|
| 620 | </div>
|
|---|
| 621 | </div>
|
|---|
| 622 |
|
|---|
| 623 |
|
|---|
| 624 | <div class="browse-horizontal">
|
|---|
| 625 |
|
|---|
| 626 | <div class="left"><a href="https://docs.djangoproject.com/en/dev/intro/tutorial02/">« Writing your first Django app, part 2</a></div>
|
|---|
| 627 |
|
|---|
| 628 |
|
|---|
| 629 | <div class="right"><a href="https://docs.djangoproject.com/en/dev/intro/tutorial04/">Writing your first Django app, part 4 »</a></div>
|
|---|
| 630 |
|
|---|
| 631 | </div>
|
|---|
| 632 | <div id="content-secondary">
|
|---|
| 633 | <h2 id="comments">Questions/Feedback</h2>
|
|---|
| 634 | <p>Having trouble? We'd like to help!</p>
|
|---|
| 635 | <ul>
|
|---|
| 636 | <li>
|
|---|
| 637 |
|
|---|
| 638 | Try the <a href="https://docs.djangoproject.com/en/dev/faq/">FAQ</a> — it's got answers to many common questions.
|
|---|
| 639 | </li>
|
|---|
| 640 | <li>
|
|---|
| 641 | Search for information in the <a href="http://groups.google.com/group/django-users/">archives of the
|
|---|
| 642 | django-users mailing list</a>, or <a href="http://groups.google.com/group/django-users/">post a question</a>.
|
|---|
| 643 | </li>
|
|---|
| 644 | <li>
|
|---|
| 645 | Ask a question in the <a href="irc://irc.freenode.net/">#django IRC
|
|---|
| 646 | channel</a>, or search the <a href="http://django-irc-logs.com/">IRC
|
|---|
| 647 | logs</a> to see if it has been asked before.
|
|---|
| 648 | </li>
|
|---|
| 649 | <li>
|
|---|
| 650 | If you notice errors with this documentation, please <a href="https://code.djangoproject.com/newticket?component=Documentation">
|
|---|
| 651 | open a ticket</a> and let us know! Please only use the ticket tracker for
|
|---|
| 652 | criticisms and improvements on the docs. For tech support, use the
|
|---|
| 653 | resources above.
|
|---|
| 654 | </li>
|
|---|
| 655 | </ul>
|
|---|
| 656 | </div>
|
|---|
| 657 |
|
|---|
| 658 | </div>
|
|---|
| 659 | <!-- END #content-main -->
|
|---|
| 660 | <div id="content-related" class="sidebar">
|
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 | <h2>Search</h2>
|
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 |
|
|---|
| 667 | <form action="/search/" id="sidebar_search" class="search">
|
|---|
| 668 | <div>
|
|---|
| 669 | <p>
|
|---|
| 670 | <input id="id_sidebar_search_q" name="q" type="search">
|
|---|
| 671 | </p>
|
|---|
| 672 | <p>
|
|---|
| 673 | <label for="id_sidebar_search_release">Version:</label>
|
|---|
| 674 | <select id="id_sidebar_search_release" name="release">
|
|---|
| 675 | <option value="2">Django 1.0</option>
|
|---|
| 676 | <option value="3">Django 1.1</option>
|
|---|
| 677 | <option value="4">Django 1.2</option>
|
|---|
| 678 | <option value="5">Django 1.3</option>
|
|---|
| 679 | <option value="6">Django 1.4</option>
|
|---|
| 680 | <option value="7">Django 1.5</option>
|
|---|
| 681 | <option value="9">Django 1.6</option>
|
|---|
| 682 | <option value="1" selected="selected">Development trunk</option>
|
|---|
| 683 | </select>
|
|---|
| 684 | </p>
|
|---|
| 685 | <p>
|
|---|
| 686 | <input class="submit" value="Search" type="submit">
|
|---|
| 687 | </p>
|
|---|
| 688 | </div>
|
|---|
| 689 | </form>
|
|---|
| 690 |
|
|---|
| 691 |
|
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 | <h2>Contents</h2>
|
|---|
| 695 |
|
|---|
| 696 | <ul>
|
|---|
| 697 | <li><a class="reference internal" href="#">Writing your first Django app, part 3</a><ul>
|
|---|
| 698 | <li><a class="reference internal" href="#philosophy">Philosophy</a></li>
|
|---|
| 699 | <li><a class="reference internal" href="#write-your-first-view">Write your first view</a><ul>
|
|---|
| 700 | <li><a class="reference internal" href="#url-argument-regex"><tt class="docutils literal"><span class="pre">url()</span></tt> argument: regex</a></li>
|
|---|
| 701 | <li><a class="reference internal" href="#url-argument-view"><tt class="docutils literal"><span class="pre">url()</span></tt> argument: view</a></li>
|
|---|
| 702 | <li><a class="reference internal" href="#url-argument-kwargs"><tt class="docutils literal"><span class="pre">url()</span></tt> argument: kwargs</a></li>
|
|---|
| 703 | <li><a class="reference internal" href="#url-argument-name"><tt class="docutils literal"><span class="pre">url()</span></tt> argument: name</a></li>
|
|---|
| 704 | </ul>
|
|---|
| 705 | </li>
|
|---|
| 706 | <li><a class="reference internal" href="#writing-more-views">Writing more views</a></li>
|
|---|
| 707 | <li><a class="reference internal" href="#write-views-that-actually-do-something">Write views that actually do something</a><ul>
|
|---|
| 708 | <li><a class="reference internal" href="#a-shortcut-render">A shortcut: <tt class="docutils literal"><span class="pre">render()</span></tt></a></li>
|
|---|
| 709 | </ul>
|
|---|
| 710 | </li>
|
|---|
| 711 | <li><a class="reference internal" href="#raising-a-404-error">Raising a 404 error</a><ul>
|
|---|
| 712 | <li><a class="reference internal" href="#a-shortcut-get-object-or-404">A shortcut: <tt class="docutils literal"><span class="pre">get_object_or_404()</span></tt></a></li>
|
|---|
| 713 | </ul>
|
|---|
| 714 | </li>
|
|---|
| 715 | <li><a class="reference internal" href="#use-the-template-system">Use the template system</a></li>
|
|---|
| 716 | <li><a class="reference internal" href="#removing-hardcoded-urls-in-templates">Removing hardcoded URLs in templates</a></li>
|
|---|
| 717 | <li><a class="reference internal" href="#namespacing-url-names">Namespacing URL names</a></li>
|
|---|
| 718 | </ul>
|
|---|
| 719 | </li>
|
|---|
| 720 | </ul>
|
|---|
| 721 |
|
|---|
| 722 |
|
|---|
| 723 |
|
|---|
| 724 |
|
|---|
| 725 | <h2>Browse</h2>
|
|---|
| 726 | <ul>
|
|---|
| 727 |
|
|---|
| 728 |
|
|---|
| 729 | <li>Prev: <a href="https://docs.djangoproject.com/en/dev/intro/tutorial02/">Writing your first Django app, part 2</a></li>
|
|---|
| 730 |
|
|---|
| 731 |
|
|---|
| 732 | <li>Next: <a href="https://docs.djangoproject.com/en/dev/intro/tutorial04/">Writing your first Django app, part 4</a></li>
|
|---|
| 733 |
|
|---|
| 734 | <li><a href="https://docs.djangoproject.com/en/dev/contents/">Table of contents</a></li>
|
|---|
| 735 |
|
|---|
| 736 | <li><a href="https://docs.djangoproject.com/en/dev/genindex/">General Index</a></li>
|
|---|
| 737 |
|
|---|
| 738 | <li><a href="https://docs.djangoproject.com/en/dev/py-modindex/">Python Module Index</a></li>
|
|---|
| 739 |
|
|---|
| 740 |
|
|---|
| 741 | </ul>
|
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 | <h2>You are here:</h2>
|
|---|
| 746 | <ul>
|
|---|
| 747 |
|
|---|
| 748 | <li>
|
|---|
| 749 | <a href="https://docs.djangoproject.com/en/dev/">Django dev documentation</a>
|
|---|
| 750 |
|
|---|
| 751 | <ul><li><a href="https://docs.djangoproject.com/en/dev/intro/">Getting started</a>
|
|---|
| 752 |
|
|---|
| 753 | <ul><li>Writing your first Django app, part 3</li></ul>
|
|---|
| 754 | </li></ul>
|
|---|
| 755 | </li>
|
|---|
| 756 |
|
|---|
| 757 | </ul>
|
|---|
| 758 |
|
|---|
| 759 |
|
|---|
| 760 | <h2>Download:</h2>
|
|---|
| 761 | <p>
|
|---|
| 762 | Offline (development version):
|
|---|
| 763 |
|
|---|
| 764 | <a href="https://docs.djangoproject.com/m/docs/django-docs-dev-en.zip">HTML</a> |
|
|---|
| 765 | <a href="http://media.readthedocs.org/pdf/django/latest/django.pdf">PDF</a> |
|
|---|
| 766 | <a href="http://media.readthedocs.org/epub/django/latest/django.epub">ePub</a>
|
|---|
| 767 | <br>
|
|---|
| 768 | <span class="quiet">
|
|---|
| 769 | Provided by <a href="http://readthedocs.org/">Read the Docs</a>.
|
|---|
| 770 | </span>
|
|---|
| 771 | </p>
|
|---|
| 772 |
|
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 | </div>
|
|---|
| 776 | <!-- END #content-related -->
|
|---|
| 777 |
|
|---|
| 778 | </div>
|
|---|
| 779 | <!-- END #content -->
|
|---|
| 780 | <div id="footer">
|
|---|
| 781 | <p>© 2005-2013 <a href="https://www.djangoproject.com/foundation/">Django Software Foundation</a> unless otherwise noted. Django is a registered trademark of the Django Software Foundation.
|
|---|
| 782 | <a href="http://mediatemple.net/">Linux Web hosting</a> graciously provided by Media Temple.
|
|---|
| 783 | </p>
|
|---|
| 784 | </div>
|
|---|
| 785 | <!-- END #footer -->
|
|---|
| 786 | </div>
|
|---|
| 787 | <!-- END #container -->
|
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 | <div id="dev-warning">
|
|---|
| 791 | This document is for Django's development version, which can be
|
|---|
| 792 | significantly different from previous releases. For older releases, use
|
|---|
| 793 | the version selector floating in the bottom right corner of this page.
|
|---|
| 794 | </div>
|
|---|
| 795 | <style type="text/css" media="screen">
|
|---|
| 796 | body {
|
|---|
| 797 | margin-top: 2em;
|
|---|
| 798 | }
|
|---|
| 799 | </style>
|
|---|
| 800 | <script src="Writing%20your%20first%20Django%20app,%20part%203%20%7C%20Django%20documentation%20%7C%20Django_files/jquery.js"></script>
|
|---|
| 801 | <script>
|
|---|
| 802 |
|
|---|
| 803 | $(document).ready(function(){
|
|---|
| 804 | // First handle the case in which someone clicks on a link
|
|---|
| 805 | $('a[href*=#]').click(function(event) {
|
|---|
| 806 | // check to see if this is an internal link, sigh.
|
|---|
| 807 | if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
|
|---|
| 808 | && location.hostname == this.hostname) {
|
|---|
| 809 | // get the link target, use the weird id= query since it escapes dots
|
|---|
| 810 | var target = $("[id='" + this.hash.slice(1) + "']");
|
|---|
| 811 | // calculate the offset
|
|---|
| 812 | var targetOffset = target.offset().top - $('#dev-warning').height() - 20;
|
|---|
| 813 | // scroll to the place, there is probably a better way
|
|---|
| 814 | setTimeout(function() {
|
|---|
| 815 | $('html,body').scrollTop(targetOffset);
|
|---|
| 816 | }, 50);
|
|---|
| 817 | }
|
|---|
| 818 | });
|
|---|
| 819 | // this is the janky thing, there is probably a better way
|
|---|
| 820 | setTimeout(function() {
|
|---|
| 821 | // is there a hash in the current window's location?
|
|---|
| 822 | if (window.location.hash) {
|
|---|
| 823 | // again, get the target
|
|---|
| 824 | var target = $("[id='" + window.location.hash.slice(1) + "']");
|
|---|
| 825 | // calculate the offset
|
|---|
| 826 | var targetOffset = target.offset().top - $('#dev-warning').height() - 20;
|
|---|
| 827 | // scroll to the place
|
|---|
| 828 | $('html,body').scrollTop(targetOffset);
|
|---|
| 829 | }
|
|---|
| 830 | // use a delay that should work on all modern computers. should, not will.
|
|---|
| 831 | }, 50);
|
|---|
| 832 | });
|
|---|
| 833 | </script>
|
|---|
| 834 |
|
|---|
| 835 |
|
|---|
| 836 |
|
|---|
| 837 |
|
|---|
| 838 |
|
|---|
| 839 | </body></html>
|
|---|