2005-07-19 05:43:07 +00:00
|
|
|
=====================================
|
|
|
|
Writing your first Django app, part 3
|
|
|
|
=====================================
|
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
This tutorial begins where :doc:`Tutorial 2 </intro/tutorial02>` left off. We're
|
2008-08-23 22:25:40 +00:00
|
|
|
continuing the Web-poll application and will focus on creating the public
|
|
|
|
interface -- "views."
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2005-08-08 15:47:57 +00:00
|
|
|
Philosophy
|
|
|
|
==========
|
2005-07-21 17:59:05 +00:00
|
|
|
|
2005-08-08 15:47:57 +00:00
|
|
|
A view is a "type" of Web page in your Django application that generally serves
|
2012-10-13 18:37:39 +00:00
|
|
|
a specific function and has a specific template. For example, in a blog
|
2005-08-08 15:47:57 +00:00
|
|
|
application, you might have the following views:
|
2005-07-22 18:45:22 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Blog homepage -- displays the latest few entries.
|
2009-06-17 13:46:52 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Entry "detail" page -- permalink page for a single entry.
|
2009-06-17 13:46:52 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Year-based archive page -- displays all months with entries in the
|
|
|
|
given year.
|
2009-06-17 13:46:52 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Month-based archive page -- displays all days with entries in the
|
|
|
|
given month.
|
2009-06-17 13:46:52 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Day-based archive page -- displays all entries in the given day.
|
2009-06-17 13:46:52 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Comment action -- handles posting comments to a given entry.
|
2005-07-22 18:45:22 +00:00
|
|
|
|
2005-08-08 15:47:57 +00:00
|
|
|
In our poll application, we'll have the following four views:
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Poll "index" page -- displays the latest few polls.
|
2009-06-17 13:46:52 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Poll "detail" page -- displays a poll question, with no results but
|
|
|
|
with a form to vote.
|
2009-06-17 13:46:52 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Poll "results" page -- displays results for a particular poll.
|
2009-06-17 13:46:52 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Vote action -- handles voting for a particular choice in a particular
|
|
|
|
poll.
|
2005-08-01 19:02:55 +00:00
|
|
|
|
2005-08-08 15:47:57 +00:00
|
|
|
In Django, each view is represented by a simple Python function.
|
2005-08-01 19:02:55 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Write your first view
|
|
|
|
=====================
|
|
|
|
|
|
|
|
Let's write the first view. Open the file ``polls/views.py``
|
|
|
|
and put the following Python code in it::
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
|
|
|
|
def index(request):
|
|
|
|
return HttpResponse("Hello, world. You're at the poll index.")
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
This is the simplest view possible in Django. Now we have a problem, how does
|
|
|
|
this view get called? For that we need to map it to a URL, in Django this is
|
|
|
|
done in a configuration file called a URLconf.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
.. admonition:: What is a URLconf?
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
In Django, web pages and other content are delivered by views and
|
|
|
|
determining which view is called is done by Python modules informally
|
|
|
|
titled 'URLconfs'. These modules are pure Python code and are a simple
|
|
|
|
mapping between URL patterns (as simple regular expressions) to Python
|
|
|
|
callback functions (your views). This tutorial provides basic instruction
|
|
|
|
in their use, and you can refer to :mod:`django.core.urlresolvers` for
|
|
|
|
more information.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
To create a URLconf in the polls directory, create a file called ``urls.py``.
|
|
|
|
Your app directory should now look like::
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
polls/
|
|
|
|
__init__.py
|
|
|
|
admin.py
|
|
|
|
models.py
|
|
|
|
tests.py
|
|
|
|
urls.py
|
|
|
|
views.py
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
In the ``polls/urls.py`` file include the following code::
|
2005-09-02 17:41:40 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
from django.conf.urls import patterns, url
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
from polls import views
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
urlpatterns = patterns('',
|
|
|
|
url(r'^$', views.index, name='index')
|
|
|
|
)
|
|
|
|
|
|
|
|
The next step is to point the root URLconf at the ``polls.urls`` module. In
|
|
|
|
``mysite/urls.py`` insert an :func:`~django.conf.urls.include`, leaving you
|
|
|
|
with::
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2011-09-11 22:36:16 +00:00
|
|
|
from django.conf.urls import patterns, include, url
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2009-06-17 13:46:52 +00:00
|
|
|
from django.contrib import admin
|
|
|
|
admin.autodiscover()
|
|
|
|
|
2005-07-19 05:43:07 +00:00
|
|
|
urlpatterns = patterns('',
|
2012-10-13 18:37:39 +00:00
|
|
|
url(r'^polls/', include('polls.urls')),
|
2011-06-26 16:52:38 +00:00
|
|
|
url(r'^admin/', include(admin.site.urls)),
|
2005-07-19 05:43:07 +00:00
|
|
|
)
|
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
You have now wired an `index` view into the URLconf. Go to
|
|
|
|
http://localhost:8000/polls/ in your browser, and you should see the text
|
|
|
|
"*Hello, world. You're at the poll index.*", which you defined in the
|
|
|
|
``index`` view.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
The :func:`~django.conf.urls.url` function is passed four arguments, two
|
|
|
|
required: ``regex`` and ``view``, and two optional: ``kwargs``, and ``name``.
|
|
|
|
At this point, it's worth reviewing what these arguments are for.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
:func:`~django.conf.urls.url` argument: regex
|
|
|
|
---------------------------------------------
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
The term `regex` is a commonly used short form meaning `regular expression`,
|
|
|
|
which is a syntax for matching patterns in strings, or in this case, url
|
|
|
|
patterns. Django starts at the first regular expression and makes its way down
|
|
|
|
the list, comparing the requested URL against each regular expression until it
|
|
|
|
finds one that matches.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2005-11-19 18:20:30 +00:00
|
|
|
Note that these regular expressions do not search GET and POST parameters, or
|
2012-10-13 18:37:39 +00:00
|
|
|
the domain name. For example, in a request to
|
|
|
|
``http://www.example.com/myapp/``, the URLconf will look for ``myapp/``. In a
|
|
|
|
request to ``http://www.example.com/myapp/?page=3``, the URLconf will also
|
|
|
|
look for ``myapp/``.
|
2005-11-19 18:20:30 +00:00
|
|
|
|
2005-07-19 05:43:07 +00:00
|
|
|
If you need help with regular expressions, see `Wikipedia's entry`_ and the
|
2011-09-04 21:17:30 +00:00
|
|
|
documentation of the :mod:`re` module. Also, the O'Reilly book "Mastering
|
2012-10-13 18:37:39 +00:00
|
|
|
Regular Expressions" by Jeffrey Friedl is fantastic. In practice, however,
|
|
|
|
you don't need to be an expert on regular expressions, as you really only need
|
|
|
|
to know how to capture simple patterns. In fact, complex regexes can have poor
|
|
|
|
lookup performance, so you probably shouldn't rely on the full power of regexes.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2006-05-15 11:33:17 +00:00
|
|
|
Finally, a performance note: these regular expressions are compiled the first
|
2012-10-13 18:37:39 +00:00
|
|
|
time the URLconf module is loaded. They're super fast (as long as the lookups
|
|
|
|
aren't too complex as noted above).
|
2005-07-19 05:43:07 +00:00
|
|
|
|
|
|
|
.. _Wikipedia's entry: http://en.wikipedia.org/wiki/Regular_expression
|
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
:func:`~django.conf.urls.url` argument: view
|
|
|
|
--------------------------------------------
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
When Django finds a regular expression match, Django calls the specified view
|
|
|
|
function, with an :class:`~django.http.HttpRequest` object as the first
|
|
|
|
argument and any “captured” values from the regular expression as other
|
|
|
|
arguments. If the regex uses simple captures, values are passed as positional
|
|
|
|
arguments; if it uses named captures, values are passed as keyword arguments.
|
|
|
|
We'll give an example of this in a bit.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
:func:`~django.conf.urls.url` argument: kwargs
|
|
|
|
----------------------------------------------
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Arbitrary keyword arguments can be passed in a dictionary to the target view. We
|
|
|
|
aren't going to use this feature of Django in the tutorial.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
:func:`~django.conf.urls.url` argument: name
|
|
|
|
---------------------------------------------
|
2005-08-01 19:02:55 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Naming your URL lets you refer to it unambiguously from elsewhere in Django
|
|
|
|
especially templates. This powerful feature allows you to make global changes
|
|
|
|
to the url patterns of your project while only touching a single file.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Writing more views
|
|
|
|
==================
|
2005-11-16 02:00:23 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Now let's add a few more views to ``polls/views.py``. These views are
|
|
|
|
slightly different, because they take an argument::
|
2005-11-16 02:00:23 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
def detail(request, poll_id):
|
|
|
|
return HttpResponse("You're looking at poll %s." % poll_id)
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
def results(request, poll_id):
|
|
|
|
return HttpResponse("You're looking at the results of poll %s." % poll_id)
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
def vote(request, poll_id):
|
|
|
|
return HttpResponse("You're voting on poll %s." % poll_id)
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Wire these news views into the ``polls.urls`` module by adding the following
|
|
|
|
:func:`~django.conf.urls.url` calls::
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
from django.conf.urls import patterns, url
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
from polls import views
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
urlpatterns = patterns('',
|
|
|
|
# ex: /polls/
|
|
|
|
url(r'^$', views.index, name='index'),
|
|
|
|
# ex: /polls/5/
|
|
|
|
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
|
|
|
|
# ex: /polls/5/results/
|
|
|
|
url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
|
|
|
|
# ex: /polls/5/vote/
|
|
|
|
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
|
|
|
|
)
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Take a look in your browser, at "/polls/34/". It'll run the ``detail()``
|
|
|
|
method and display whatever ID you provide in the URL. Try
|
|
|
|
"/polls/34/results/" and "/polls/34/vote/" too -- these will display the
|
|
|
|
placeholder results and voting pages.
|
|
|
|
|
|
|
|
When somebody requests a page from your Web site -- say, "/polls/34/", Django
|
|
|
|
will load the ``mysite.urls`` Python module because it's pointed to by the
|
|
|
|
:setting:`ROOT_URLCONF` setting. It finds the variable named ``urlpatterns``
|
|
|
|
and traverses the regular expressions in order. The
|
|
|
|
:func:`~django.conf.urls.include` functions we are using simply reference
|
|
|
|
other URLconfs. Note that the regular expressions for the
|
|
|
|
:func:`~django.conf.urls.include` functions don't have a ``$`` (end-of-string
|
|
|
|
match character) but rather a trailing slash. Whenever Django encounters
|
|
|
|
:func:`~django.conf.urls.include`, it chops off whatever part of the URL
|
|
|
|
matched up to that point and sends the remaining string to the included
|
|
|
|
URLconf for further processing.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
The idea behind :func:`~django.conf.urls.include` is to make it easy to
|
|
|
|
plug-and-play URLs. Since polls are in their own URLconf
|
|
|
|
(``polls/urls.py``), they can be placed under "/polls/", or under
|
|
|
|
"/fun_polls/", or under "/content/polls/", or any other path root, and the
|
|
|
|
app will still work.
|
2009-10-14 13:38:31 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Here's what happens if a user goes to "/polls/34/" in this system:
|
2009-10-14 13:38:31 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
* Django will find the match at ``'^polls/'``
|
|
|
|
|
|
|
|
* Then, Django will strip off the matching text (``"polls/"``) and send the
|
|
|
|
remaining text -- ``"34/"`` -- to the 'polls.urls' URLconf for
|
|
|
|
further processing which matches ``r'^(?P<poll_id>\d+)/$'`` resulting in a
|
|
|
|
call to the ``detail()`` view like so::
|
|
|
|
|
|
|
|
detail(request=<HttpRequest object>, poll_id='34')
|
|
|
|
|
|
|
|
The ``poll_id='34'`` part comes from ``(?P<poll_id>\d+)``. Using parentheses
|
|
|
|
around a pattern "captures" the text matched by that pattern and sends it as an
|
|
|
|
argument to the view function; ``?P<poll_id>`` defines the name that will
|
|
|
|
be used to identify the matched pattern; and ``\d+`` is a regular expression to
|
|
|
|
match a sequence of digits (i.e., a number).
|
|
|
|
|
|
|
|
Because the URL patterns are regular expressions, there really is no limit on
|
|
|
|
what you can do with them. And there's no need to add URL cruft such as ``.php``
|
|
|
|
-- unless you have a sick sense of humor, in which case you can do something
|
|
|
|
like this::
|
|
|
|
|
|
|
|
(r'^polls/latest\.php$', 'polls.views.index'),
|
|
|
|
|
|
|
|
But, don't do that. It's silly.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
|
|
|
Write views that actually do something
|
|
|
|
======================================
|
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Each view is responsible for doing one of two things: returning an
|
2008-08-23 22:25:40 +00:00
|
|
|
:class:`~django.http.HttpResponse` object containing the content for the
|
|
|
|
requested page, or raising an exception such as :exc:`~django.http.Http404`. The
|
|
|
|
rest is up to you.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
|
|
|
Your view can read records from a database, or not. It can use a template
|
|
|
|
system such as Django's -- or a third-party Python template system -- or not.
|
|
|
|
It can generate a PDF file, output XML, create a ZIP file on the fly, anything
|
|
|
|
you want, using whatever Python libraries you want.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
All Django wants is that :class:`~django.http.HttpResponse`. Or an exception.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
|
|
|
Because it's convenient, let's use Django's own database API, which we covered
|
2010-08-19 19:27:44 +00:00
|
|
|
in :doc:`Tutorial 1 </intro/tutorial01>`. Here's one stab at the ``index()``
|
2008-08-23 22:25:40 +00:00
|
|
|
view, which displays the latest 5 poll questions in the system, separated by
|
|
|
|
commas, according to publication date::
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
from django.http import HttpResponse
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
from polls.models import Poll
|
|
|
|
|
2005-07-19 05:43:07 +00:00
|
|
|
def index(request):
|
2012-10-13 18:37:39 +00:00
|
|
|
latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
|
2005-07-19 05:43:07 +00:00
|
|
|
output = ', '.join([p.question for p in latest_poll_list])
|
|
|
|
return HttpResponse(output)
|
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
There's a problem here, though: the page's design is hard-coded in the view. If
|
2005-07-19 05:43:07 +00:00
|
|
|
you want to change the way the page looks, you'll have to edit this Python code.
|
2012-10-13 18:37:39 +00:00
|
|
|
So let's use Django's template system to separate the design from Python.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
First, create a directory ``polls`` in your template directory you specified
|
|
|
|
in setting:`TEMPLATE_DIRS`. Within that, create a file called ``index.html``.
|
2008-08-23 22:25:40 +00:00
|
|
|
Put the following code in that template:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-07-19 05:43:07 +00:00
|
|
|
|
|
|
|
{% if latest_poll_list %}
|
|
|
|
<ul>
|
|
|
|
{% for poll in latest_poll_list %}
|
2010-02-24 22:11:49 +00:00
|
|
|
<li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>
|
2005-07-19 05:43:07 +00:00
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
{% else %}
|
|
|
|
<p>No polls are available.</p>
|
|
|
|
{% endif %}
|
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Now let's use that html template in our index view::
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.template import Context, loader
|
|
|
|
|
|
|
|
from polls.models import Poll
|
|
|
|
|
|
|
|
def index(request):
|
|
|
|
latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
|
|
|
|
template = loader.get_template('polls/index.html')
|
|
|
|
context = Context({
|
|
|
|
'latest_poll_list': latest_poll_list,
|
|
|
|
})
|
|
|
|
return HttpResponse(template.render(context))
|
|
|
|
|
|
|
|
That code loads the template called ``polls/index.html`` and passes it a
|
|
|
|
context. The context is a dictionary mapping template variable names to Python
|
|
|
|
objects.
|
|
|
|
|
2005-07-21 15:36:43 +00:00
|
|
|
Load the page in your Web browser, and you should see a bulleted-list
|
2010-02-23 15:00:33 +00:00
|
|
|
containing the "What's up" poll from Tutorial 1. The link points to the poll's
|
|
|
|
detail page.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-30 19:53:56 +00:00
|
|
|
.. admonition:: Organizing Templates
|
|
|
|
|
|
|
|
Rather than one big templates directory, you can also store templates
|
|
|
|
within each app. We'll discuss this in more detail in the :doc:`reusable
|
|
|
|
apps tutorial</intro/reusable-apps>`.
|
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
A shortcut: :func:`~django.shortcuts.render`
|
|
|
|
--------------------------------------------
|
2005-09-23 22:50:05 +00:00
|
|
|
|
|
|
|
It's a very common idiom to load a template, fill a context and return an
|
2008-08-23 22:25:40 +00:00
|
|
|
:class:`~django.http.HttpResponse` object with the result of the rendered
|
|
|
|
template. Django provides a shortcut. Here's the full ``index()`` view,
|
|
|
|
rewritten::
|
2005-09-23 22:50:05 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
|
2010-10-09 07:45:52 +00:00
|
|
|
from polls.models import Poll
|
2005-09-23 22:50:05 +00:00
|
|
|
|
|
|
|
def index(request):
|
2006-10-23 07:42:04 +00:00
|
|
|
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
|
2012-10-13 18:37:39 +00:00
|
|
|
context = {'latest_poll_list': latest_poll_list}
|
|
|
|
return render(request, 'polls/index.html', context)
|
2005-09-23 22:50:05 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Note that once we've done this in all these views, we no longer need to import
|
|
|
|
:mod:`~django.template.loader`, :class:`~django.template.Context` and
|
2012-10-13 18:37:39 +00:00
|
|
|
:class:`~django.http.HttpResponse` (you'll want to keep ``HttpResponse`` if you
|
|
|
|
still have the stub methods for ``detail``, ``results``, and ``vote``).
|
2005-09-23 22:50:05 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
The :func:`~django.shortcuts.render` function takes the request object as its
|
|
|
|
first argument, a template name as its second argument and a dictionary as its
|
|
|
|
optional third argument. It returns an :class:`~django.http.HttpResponse`
|
|
|
|
object of the given template rendered with the given context.
|
2005-09-23 22:50:05 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Raising a 404 error
|
|
|
|
===================
|
2005-07-19 05:43:07 +00:00
|
|
|
|
|
|
|
Now, let's tackle the poll detail view -- the page that displays the question
|
|
|
|
for a given poll. Here's the view::
|
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
from django.http import Http404
|
|
|
|
# ...
|
2005-07-19 05:43:07 +00:00
|
|
|
def detail(request, poll_id):
|
|
|
|
try:
|
2012-10-13 18:37:39 +00:00
|
|
|
poll = Poll.objects.get(pk=poll_id)
|
2006-05-02 01:31:56 +00:00
|
|
|
except Poll.DoesNotExist:
|
2005-07-19 05:43:07 +00:00
|
|
|
raise Http404
|
2012-10-13 18:37:39 +00:00
|
|
|
return render(request, 'polls/detail.html', {'poll': poll})
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
The new concept here: The view raises the :exc:`~django.http.Http404` exception
|
|
|
|
if a poll with the requested ID doesn't exist.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-03 18:30:54 +00:00
|
|
|
We'll discuss what you could put in that ``polls/detail.html`` template a bit
|
|
|
|
later, but if you'd like to quickly get the above example working, just::
|
|
|
|
|
|
|
|
{{ poll }}
|
2009-06-17 13:46:52 +00:00
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-03 18:30:54 +00:00
|
|
|
will get you started for now.
|
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
A shortcut: :func:`~django.shortcuts.get_object_or_404`
|
|
|
|
-------------------------------------------------------
|
2005-09-23 22:50:05 +00:00
|
|
|
|
2011-09-30 10:28:39 +00:00
|
|
|
It's a very common idiom to use :meth:`~django.db.models.query.QuerySet.get`
|
|
|
|
and raise :exc:`~django.http.Http404` if the object doesn't exist. Django
|
|
|
|
provides a shortcut. Here's the ``detail()`` view, rewritten::
|
2005-09-23 22:50:05 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
from django.shortcuts import render, get_object_or_404
|
2006-05-02 01:31:56 +00:00
|
|
|
# ...
|
2005-09-23 22:50:05 +00:00
|
|
|
def detail(request, poll_id):
|
2012-10-13 18:37:39 +00:00
|
|
|
poll = get_object_or_404(Poll, pk=poll_id)
|
|
|
|
return render(request, 'polls/detail.html', {'poll': poll})
|
2005-09-23 22:50:05 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
The :func:`~django.shortcuts.get_object_or_404` function takes a Django model
|
2009-07-28 14:38:11 +00:00
|
|
|
as its first argument and an arbitrary number of keyword arguments, which it
|
2012-05-11 10:43:43 +00:00
|
|
|
passes to the :meth:`~django.db.models.query.QuerySet.get` function of the
|
|
|
|
model's manager. It raises :exc:`~django.http.Http404` if the object doesn't
|
|
|
|
exist.
|
2005-09-23 22:50:05 +00:00
|
|
|
|
|
|
|
.. admonition:: Philosophy
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Why do we use a helper function :func:`~django.shortcuts.get_object_or_404`
|
|
|
|
instead of automatically catching the
|
|
|
|
:exc:`~django.core.exceptions.ObjectDoesNotExist` exceptions at a higher
|
|
|
|
level, or having the model API raise :exc:`~django.http.Http404` instead of
|
|
|
|
:exc:`~django.core.exceptions.ObjectDoesNotExist`?
|
2005-09-23 22:50:05 +00:00
|
|
|
|
|
|
|
Because that would couple the model layer to the view layer. One of the
|
2012-10-13 18:37:39 +00:00
|
|
|
foremost design goals of Django is to maintain loose coupling. Some
|
|
|
|
controlled coupling is introduced in the :mod:`django.shortcuts` module.
|
2005-09-23 22:50:05 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
There's also a :func:`~django.shortcuts.get_list_or_404` function, which works
|
|
|
|
just as :func:`~django.shortcuts.get_object_or_404` -- except using
|
2011-09-30 10:28:39 +00:00
|
|
|
:meth:`~django.db.models.query.QuerySet.filter` instead of
|
|
|
|
:meth:`~django.db.models.query.QuerySet.get`. It raises
|
|
|
|
:exc:`~django.http.Http404` if the list is empty.
|
2005-09-23 22:50:05 +00:00
|
|
|
|
2005-07-19 05:43:07 +00:00
|
|
|
Write a 404 (page not found) view
|
|
|
|
=================================
|
|
|
|
|
2011-09-11 04:01:41 +00:00
|
|
|
When you raise :exc:`~django.http.Http404` from within a view, Django
|
|
|
|
will load a special view devoted to handling 404 errors. It finds it
|
|
|
|
by looking for the variable ``handler404`` in your root URLconf (and
|
|
|
|
only in your root URLconf; setting ``handler404`` anywhere else will
|
|
|
|
have no effect), which is a string in Python dotted syntax -- the same
|
|
|
|
format the normal URLconf callbacks use. A 404 view itself has nothing
|
|
|
|
special: It's just a normal view.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-01-12 22:03:34 +00:00
|
|
|
You normally won't have to bother with writing 404 views. If you don't set
|
|
|
|
``handler404``, the built-in view :func:`django.views.defaults.page_not_found`
|
2012-09-30 21:16:14 +00:00
|
|
|
is used by default. Optionally, you can create a ``404.html`` template
|
|
|
|
in the root of your template directory. The default 404 view will then use that
|
|
|
|
template for all 404 errors when :setting:`DEBUG` is set to ``False`` (in your
|
2012-10-13 18:37:39 +00:00
|
|
|
settings module). If you do create the template, add at least some dummy
|
|
|
|
content like "Page not found".
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-01-12 22:03:34 +00:00
|
|
|
A couple more things to note about 404 views:
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* If :setting:`DEBUG` is set to ``True`` (in your settings module) then your
|
|
|
|
404 view will never be used (and thus the ``404.html`` template will never
|
|
|
|
be rendered) because the traceback will be displayed instead.
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-03 18:30:54 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* The 404 view is also called if Django doesn't find a match after checking
|
|
|
|
every regular expression in the URLconf.
|
2009-06-17 13:46:52 +00:00
|
|
|
|
2005-07-19 05:43:07 +00:00
|
|
|
Write a 500 (server error) view
|
|
|
|
===============================
|
|
|
|
|
2011-09-11 04:01:41 +00:00
|
|
|
Similarly, your root URLconf may define a ``handler500``, which points
|
|
|
|
to a view to call in case of server errors. Server errors happen when
|
|
|
|
you have runtime errors in view code.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Likewise, you should create a ``500.html`` template at the root of your
|
|
|
|
template directory and add some content like "Something went wrong".
|
|
|
|
|
2005-07-19 05:43:07 +00:00
|
|
|
Use the template system
|
|
|
|
=======================
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Back to the ``detail()`` view for our poll application. Given the context
|
2012-10-13 18:37:39 +00:00
|
|
|
variable ``poll``, here's what the ``polls/detail.html`` template might look
|
2008-08-23 22:25:40 +00:00
|
|
|
like:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-07-19 05:43:07 +00:00
|
|
|
|
|
|
|
<h1>{{ poll.question }}</h1>
|
|
|
|
<ul>
|
2006-05-02 01:31:56 +00:00
|
|
|
{% for choice in poll.choice_set.all %}
|
2012-05-25 19:13:51 +00:00
|
|
|
<li>{{ choice.choice_text }}</li>
|
2005-07-19 05:43:07 +00:00
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
The template system uses dot-lookup syntax to access variable attributes. In
|
|
|
|
the example of ``{{ poll.question }}``, first Django does a dictionary lookup
|
2011-01-13 13:47:21 +00:00
|
|
|
on the object ``poll``. Failing that, it tries an attribute lookup -- which
|
|
|
|
works, in this case. If attribute lookup had failed, it would've tried a
|
|
|
|
list-index lookup.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
Method-calling happens in the :ttag:`{% for %}<for>` loop:
|
|
|
|
``poll.choice_set.all`` is interpreted as the Python code
|
2012-05-25 19:13:51 +00:00
|
|
|
``poll.choice_set.all()``, which returns an iterable of ``Choice`` objects and is
|
2011-10-03 08:06:01 +00:00
|
|
|
suitable for use in the :ttag:`{% for %}<for>` tag.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
See the :doc:`template guide </topics/templates>` for more about templates.
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Removing hardcoded URLs in templates
|
|
|
|
====================================
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Remember, when we wrote the link to a poll in the ``polls/index.html``
|
|
|
|
template, the link was partially hardcoded like this:
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
.. code-block:: html+django
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
<li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
The problem with this hardcoded, tightly-coupled approach is that it becomes
|
|
|
|
challenging to change URLs on projects with a lot of templates. However, since
|
|
|
|
you defined the name argument in the :func:`~django.conf.urls.url` functions in
|
|
|
|
the ``polls.urls`` module, you can remove a reliance on specific URL paths
|
|
|
|
defined in your url configurations by using the ``{% url %}`` template tag:
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
.. code-block:: html+django
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
<li><a href="{% url 'detail' poll.id %}">{{ poll.question }}</a></li>
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
.. note::
|
2010-11-06 10:06:30 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
If ``{% url 'detail' poll.id %}`` (with quotes) doesn't work, but
|
|
|
|
``{% url detail poll.id %}`` (without quotes) does, that means you're
|
|
|
|
using a version of Django < 1.5. In this case, add the following
|
|
|
|
declaration at the top of your template:
|
2010-11-06 10:06:30 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
.. code-block:: html+django
|
2011-09-11 22:36:16 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
{% load url from future %}
|
2011-09-11 22:36:16 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
The way this works is by looking up the URL definition as specified in the
|
|
|
|
``polls.urls`` module. You can see exactly where the URL name of 'detail' is
|
|
|
|
defined below::
|
2010-11-06 10:06:30 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
...
|
|
|
|
# the 'name' value as called by the {% url %} template tag
|
|
|
|
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
|
|
|
|
...
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
If you want to change the URL of the polls detail view to something else,
|
|
|
|
perhaps to something like ``polls/specifics/12/`` instead of doing it in the
|
|
|
|
template (or templates) you would change it in ``polls/urls.py``::
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
...
|
|
|
|
# added the word 'specifics'
|
|
|
|
url(r'^specifics/(?P<poll_id>\d+)/$', views.detail, name='detail'),
|
|
|
|
...
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Namespacing URL names
|
|
|
|
======================
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
The tutorial project has just one app, ``polls``. In real Django projects,
|
|
|
|
there might be five, ten, twenty apps or more. How does Django differentiate
|
|
|
|
the URL names between them? For example, the ``polls`` app has a ``detail``
|
|
|
|
view, and so might an app on the same project that is for a blog. How does one
|
|
|
|
make it so that Django knows which app view to create for a url when using the
|
|
|
|
``{% url %}`` template tag?
|
|
|
|
|
|
|
|
The answer is to add namespaces to your root URLconf. In the
|
|
|
|
``mysite/urls.py`` file, go ahead and change it to include namespacing::
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2011-09-11 22:36:16 +00:00
|
|
|
from django.conf.urls import patterns, include, url
|
|
|
|
|
2010-11-06 10:06:30 +00:00
|
|
|
from django.contrib import admin
|
|
|
|
admin.autodiscover()
|
2011-09-11 22:36:16 +00:00
|
|
|
|
2009-06-18 13:36:40 +00:00
|
|
|
urlpatterns = patterns('',
|
2012-10-13 18:37:39 +00:00
|
|
|
url(r'^polls/', include('polls.urls', namespace="polls")),
|
2011-06-26 16:52:38 +00:00
|
|
|
url(r'^admin/', include(admin.site.urls)),
|
2010-09-13 22:47:20 +00:00
|
|
|
)
|
2005-07-19 05:43:07 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
Now change your ``polls/index.html`` template from:
|
2012-07-29 22:14:26 +00:00
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
<li><a href="{% url 'detail' poll.id %}">{{ poll.question }}</a></li>
|
2012-07-29 22:14:26 +00:00
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
to point at the namespaced detail view:
|
2012-07-29 22:14:26 +00:00
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
2012-10-13 18:37:39 +00:00
|
|
|
<li><a href="{% url 'polls:detail' poll.id %}">{{ poll.question }}</a></li>
|
2012-08-18 07:55:13 +00:00
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
When you're comfortable with writing views, read :doc:`part 4 of this tutorial
|
|
|
|
</intro/tutorial04>` to learn about simple form processing and generic views.
|