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:
parent
a469d821a1
commit
464e84257d
|
@ -0,0 +1 @@
|
||||||
|
# Create your views here.
|
|
@ -131,8 +131,7 @@ That'll create a directory structure like this::
|
||||||
models/
|
models/
|
||||||
__init__.py
|
__init__.py
|
||||||
polls.py
|
polls.py
|
||||||
views/
|
views.py
|
||||||
__init__.py
|
|
||||||
|
|
||||||
This directory structure will house the poll application.
|
This directory structure will house the poll application.
|
||||||
|
|
||||||
|
|
|
@ -73,10 +73,10 @@ this::
|
||||||
from django.conf.urls.defaults import *
|
from django.conf.urls.defaults import *
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
(r'^polls/$', 'myproject.apps.polls.views.polls.index'),
|
(r'^polls/$', 'myproject.apps.polls.views.index'),
|
||||||
(r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views.polls.detail'),
|
(r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views.detail'),
|
||||||
(r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.apps.polls.views.polls.results'),
|
(r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.apps.polls.views.results'),
|
||||||
(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'),
|
||||||
)
|
)
|
||||||
|
|
||||||
This is worth a review. When somebody requests a page from your Web site --
|
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``
|
by the ``ROOT_URLCONF`` setting. It finds the variable named ``urlpatterns``
|
||||||
and traverses the regular expressions in order. When it finds a regular
|
and traverses the regular expressions in order. When it finds a regular
|
||||||
expression that matches -- ``r'^polls/(?P<poll_id>\d+)/$'`` -- it loads the
|
expression that matches -- ``r'^polls/(?P<poll_id>\d+)/$'`` -- it loads the
|
||||||
associated Python package/module: ``myproject.apps.polls.views.polls.detail``. That
|
associated Python package/module: ``myproject.apps.polls.views.detail``. That
|
||||||
corresponds to the function ``detail()`` in ``myproject/apps/polls/views/polls.py``.
|
corresponds to the function ``detail()`` in ``myproject/apps/polls/views.py``.
|
||||||
Finally, it calls that ``detail()`` function like so::
|
Finally, it calls that ``detail()`` function like so::
|
||||||
|
|
||||||
detail(request=<HttpRequest object>, poll_id=23)
|
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
|
``.php`` -- unless you have a sick sense of humor, in which case you can do
|
||||||
something like this::
|
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
|
If you need help with regular expressions, see `Wikipedia's entry`_ and the
|
||||||
`Python documentation`_. Also, the O'Reilly book "Mastering Regular
|
`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
|
django-admin.py runserver --settings=myproject.settings
|
||||||
|
|
||||||
Now go to "http://localhost:8000/polls/" on your domain in your Web browser.
|
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
|
ViewDoesNotExist at /polls/
|
||||||
was: No module named 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
|
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
|
messages tell you which view Django tried (and failed to find, because you
|
||||||
you haven't written any views yet).
|
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::
|
and put the following Python code in it::
|
||||||
|
|
||||||
from django.utils.httpwrappers import HttpResponse
|
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::
|
the URLconf, you may notice there's a fair bit of redundancy in it::
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
(r'^polls/$', 'myproject.apps.polls.views.polls.index'),
|
(r'^polls/$', 'myproject.apps.polls.views.index'),
|
||||||
(r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views.polls.detail'),
|
(r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views.detail'),
|
||||||
(r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.apps.polls.views.polls.results'),
|
(r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.apps.polls.views.results'),
|
||||||
(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'),
|
||||||
)
|
)
|
||||||
|
|
||||||
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
|
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
|
common prefixes. You can factor out the common prefixes and add them as the
|
||||||
first argument to ``patterns()``, like so::
|
first argument to ``patterns()``, like so::
|
||||||
|
|
||||||
urlpatterns = patterns('myproject.apps.polls.views.polls',
|
urlpatterns = patterns('myproject.apps.polls.views',
|
||||||
(r'^polls/$', 'index'),
|
(r'^polls/$', 'index'),
|
||||||
(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
|
(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
|
||||||
(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
|
(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
|
'myproject.apps.polls.urls' urlconf by removing the leading "polls/" from each
|
||||||
line::
|
line::
|
||||||
|
|
||||||
urlpatterns = patterns('myproject.apps.polls.views.polls',
|
urlpatterns = patterns('myproject.apps.polls.views',
|
||||||
(r'^$', 'index'),
|
(r'^$', 'index'),
|
||||||
(r'^(?P<poll_id>\d+)/$', 'detail'),
|
(r'^(?P<poll_id>\d+)/$', 'detail'),
|
||||||
(r'^(?P<poll_id>\d+)/results/$', 'results'),
|
(r'^(?P<poll_id>\d+)/results/$', 'results'),
|
||||||
|
|
|
@ -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
|
something with it. Remember, in `Tutorial 3`_, we create a URLconf that
|
||||||
included this line::
|
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.core.extensions import get_object_or_404, render_to_response
|
||||||
from django.models.polls import choices, polls
|
from django.models.polls import choices, polls
|
||||||
|
@ -158,7 +158,7 @@ so far::
|
||||||
|
|
||||||
from django.conf.urls.defaults import *
|
from django.conf.urls.defaults import *
|
||||||
|
|
||||||
urlpatterns = patterns('myproject.apps.polls.views.polls',
|
urlpatterns = patterns('myproject.apps.polls.views',
|
||||||
(r'^$', 'index'),
|
(r'^$', 'index'),
|
||||||
(r'^(?P<poll_id>\d+)/$', 'detail'),
|
(r'^(?P<poll_id>\d+)/$', 'detail'),
|
||||||
(r'^(?P<poll_id>\d+)/results/$', 'results'),
|
(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'^$', '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+)/$', '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<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``.
|
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``.
|
``polls/polls_detail``, and pass ``object`` in the context instead of ``poll``.
|
||||||
|
|
||||||
Finally, you can delete the ``index()``, ``detail()`` and ``results()`` views
|
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`_.
|
For full details on generic views, see the `generic views documentation`_.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue