Changed 'django-admin.py startapp' application template to use views.py instead of views package, for simplicity. Updated tutorial to reflect the change.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1258 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-11-16 02:00:23 +00:00
parent a469d821a1
commit 464e84257d
5 changed files with 33 additions and 28 deletions

View File

@ -0,0 +1 @@
# Create your views here.

View File

@ -131,8 +131,7 @@ That'll create a directory structure like this::
models/
__init__.py
polls.py
views/
__init__.py
views.py
This directory structure will house the poll application.

View File

@ -73,10 +73,10 @@ this::
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^polls/$', 'myproject.apps.polls.views.polls.index'),
(r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views.polls.detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.apps.polls.views.polls.results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views.polls.vote'),
(r'^polls/$', 'myproject.apps.polls.views.index'),
(r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views.detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.apps.polls.views.results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views.vote'),
)
This is worth a review. When somebody requests a page from your Web site --
@ -84,8 +84,8 @@ say, "/polls/23/", Django will load this Python module, because it's pointed to
by the ``ROOT_URLCONF`` setting. It finds the variable named ``urlpatterns``
and traverses the regular expressions in order. When it finds a regular
expression that matches -- ``r'^polls/(?P<poll_id>\d+)/$'`` -- it loads the
associated Python package/module: ``myproject.apps.polls.views.polls.detail``. That
corresponds to the function ``detail()`` in ``myproject/apps/polls/views/polls.py``.
associated Python package/module: ``myproject.apps.polls.views.detail``. That
corresponds to the function ``detail()`` in ``myproject/apps/polls/views.py``.
Finally, it calls that ``detail()`` function like so::
detail(request=<HttpRequest object>, poll_id=23)
@ -99,9 +99,9 @@ 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$', 'myproject.apps.polls.views.polls.index'),
(r'^polls/latest\.php$', 'myproject.apps.polls.views.index'),
But, don't do that. It's stupid.
But, don't do that. It's silly.
If you need help with regular expressions, see `Wikipedia's entry`_ and the
`Python documentation`_. Also, the O'Reilly book "Mastering Regular
@ -125,16 +125,21 @@ Fire up the Django development Web server::
django-admin.py runserver --settings=myproject.settings
Now go to "http://localhost:8000/polls/" on your domain in your Web browser.
You should get a Python traceback with the following error message::
You should get a pleasantly-colored error page with the following message::
ViewDoesNotExist: Could not import myproject.apps.polls.views.polls. Error
was: No module named polls
ViewDoesNotExist at /polls/
Tried index in module myproject.apps.polls.views. Error was: 'module'
object has no attribute 'index'
This error happened because you haven't written a function ``index()`` in the
module ``myproject/apps/polls/views.py``.
Try "/polls/23/", "/polls/23/results/" and "/polls/23/vote/". The error
messages should tell you which view Django tried (and failed to find, because
you haven't written any views yet).
messages tell you which view Django tried (and failed to find, because you
haven't written any views yet).
Time to write the first view. Create the file ``myproject/apps/polls/views/polls.py``
Time to write the first view. Open the file ``myproject/apps/polls/views.py``
and put the following Python code in it::
from django.utils.httpwrappers import HttpResponse
@ -374,19 +379,19 @@ Take some time to play around with the views and template system. As you edit
the URLconf, you may notice there's a fair bit of redundancy in it::
urlpatterns = patterns('',
(r'^polls/$', 'myproject.apps.polls.views.polls.index'),
(r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views.polls.detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.apps.polls.views.polls.results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views.polls.vote'),
(r'^polls/$', 'myproject.apps.polls.views.index'),
(r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views.detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.apps.polls.views.results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views.vote'),
)
Namely, ``myproject.apps.polls.views.polls`` is in every callback.
Namely, ``myproject.apps.polls.views`` is in every callback.
Because this is a common case, the URLconf framework provides a shortcut for
common prefixes. You can factor out the common prefixes and add them as the
first argument to ``patterns()``, like so::
urlpatterns = patterns('myproject.apps.polls.views.polls',
urlpatterns = patterns('myproject.apps.polls.views',
(r'^polls/$', 'index'),
(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
@ -435,7 +440,7 @@ Now that we've decoupled that, we need to decouple the
'myproject.apps.polls.urls' urlconf by removing the leading "polls/" from each
line::
urlpatterns = patterns('myproject.apps.polls.views.polls',
urlpatterns = patterns('myproject.apps.polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),

View File

@ -44,9 +44,9 @@ Now, let's create a Django view that handles the submitted data and does
something with it. Remember, in `Tutorial 3`_, we create a URLconf that
included this line::
(r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views.polls.vote'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views.vote'),
So let's create a ``vote()`` function in ``myproject/apps/polls/views/polls.py``::
So let's create a ``vote()`` function in ``myproject/apps/polls/views.py``::
from django.core.extensions import get_object_or_404, render_to_response
from django.models.polls import choices, polls
@ -158,7 +158,7 @@ so far::
from django.conf.urls.defaults import *
urlpatterns = patterns('myproject.apps.polls.views.polls',
urlpatterns = patterns('myproject.apps.polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
@ -178,7 +178,7 @@ Change it like so::
(r'^$', 'django.views.generic.list_detail.object_list', info_dict),
(r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
(r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='polls/results')),
(r'^(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views.polls.vote'),
(r'^(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views.vote'),
)
We're using two generic views here: ``object_list`` and ``object_detail``.
@ -217,7 +217,7 @@ In the ``vote()`` view, change the template call from ``polls/detail`` to
``polls/polls_detail``, and pass ``object`` in the context instead of ``poll``.
Finally, you can delete the ``index()``, ``detail()`` and ``results()`` views
from ``polls/views/polls.py``. We don't need them anymore.
from ``polls/views.py``. We don't need them anymore.
For full details on generic views, see the `generic views documentation`_.