Changeset 1258
- Timestamp:
- 11/15/05 20:00:23 (3 years ago)
- Files:
-
- django/trunk/django/conf/app_template/views (deleted)
- django/trunk/django/conf/app_template/views.py (added)
- django/trunk/docs/tutorial01.txt (modified) (1 diff)
- django/trunk/docs/tutorial03.txt (modified) (7 diffs)
- django/trunk/docs/tutorial04.txt (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/docs/tutorial01.txt
r948 r1258 132 132 __init__.py 133 133 polls.py 134 views/ 135 __init__.py 134 views.py 136 135 137 136 This directory structure will house the poll application. django/trunk/docs/tutorial03.txt
r1210 r1258 74 74 75 75 urlpatterns = patterns('', 76 (r'^polls/$', 'myproject.apps.polls.views. polls.index'),77 (r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views. polls.detail'),78 (r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.apps.polls.views. polls.results'),79 (r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views. polls.vote'),76 (r'^polls/$', 'myproject.apps.polls.views.index'), 77 (r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views.detail'), 78 (r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.apps.polls.views.results'), 79 (r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views.vote'), 80 80 ) 81 81 … … 85 85 and traverses the regular expressions in order. When it finds a regular 86 86 expression that matches -- ``r'^polls/(?P<poll_id>\d+)/$'`` -- it loads the 87 associated Python package/module: ``myproject.apps.polls.views. polls.detail``. That88 corresponds to the function ``detail()`` in ``myproject/apps/polls/views /polls.py``.87 associated Python package/module: ``myproject.apps.polls.views.detail``. That 88 corresponds to the function ``detail()`` in ``myproject/apps/polls/views.py``. 89 89 Finally, it calls that ``detail()`` function like so:: 90 90 … … 100 100 something like this:: 101 101 102 (r'^polls/latest\.php$', 'myproject.apps.polls.views. polls.index'),103 104 But, don't do that. It's s tupid.102 (r'^polls/latest\.php$', 'myproject.apps.polls.views.index'), 103 104 But, don't do that. It's silly. 105 105 106 106 If you need help with regular expressions, see `Wikipedia's entry`_ and the … … 126 126 127 127 Now go to "http://localhost:8000/polls/" on your domain in your Web browser. 128 You should get a Python traceback with the following error message:: 129 130 ViewDoesNotExist: Could not import myproject.apps.polls.views.polls. Error 131 was: No module named polls 128 You should get a pleasantly-colored error page with the following message:: 129 130 ViewDoesNotExist at /polls/ 131 132 Tried index in module myproject.apps.polls.views. Error was: 'module' 133 object has no attribute 'index' 134 135 This error happened because you haven't written a function ``index()`` in the 136 module ``myproject/apps/polls/views.py``. 132 137 133 138 Try "/polls/23/", "/polls/23/results/" and "/polls/23/vote/". The error 134 messages should tell you which view Django tried (and failed to find, because135 youhaven't written any views yet).136 137 Time to write the first view. Create the file ``myproject/apps/polls/views/polls.py``139 messages tell you which view Django tried (and failed to find, because you 140 haven't written any views yet). 141 142 Time to write the first view. Open the file ``myproject/apps/polls/views.py`` 138 143 and put the following Python code in it:: 139 144 … … 375 380 376 381 urlpatterns = patterns('', 377 (r'^polls/$', 'myproject.apps.polls.views. polls.index'),378 (r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views. polls.detail'),379 (r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.apps.polls.views. polls.results'),380 (r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views. polls.vote'),382 (r'^polls/$', 'myproject.apps.polls.views.index'), 383 (r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views.detail'), 384 (r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.apps.polls.views.results'), 385 (r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views.vote'), 381 386 ) 382 387 383 Namely, ``myproject.apps.polls.views .polls`` is in every callback.388 Namely, ``myproject.apps.polls.views`` is in every callback. 384 389 385 390 Because this is a common case, the URLconf framework provides a shortcut for … … 387 392 first argument to ``patterns()``, like so:: 388 393 389 urlpatterns = patterns('myproject.apps.polls.views .polls',394 urlpatterns = patterns('myproject.apps.polls.views', 390 395 (r'^polls/$', 'index'), 391 396 (r'^polls/(?P<poll_id>\d+)/$', 'detail'), … … 436 441 line:: 437 442 438 urlpatterns = patterns('myproject.apps.polls.views .polls',443 urlpatterns = patterns('myproject.apps.polls.views', 439 444 (r'^$', 'index'), 440 445 (r'^(?P<poll_id>\d+)/$', 'detail'), django/trunk/docs/tutorial04.txt
r1034 r1258 45 45 included this line:: 46 46 47 (r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views. polls.vote'),48 49 So let's create a ``vote()`` function in ``myproject/apps/polls/views /polls.py``::47 (r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views.vote'), 48 49 So let's create a ``vote()`` function in ``myproject/apps/polls/views.py``:: 50 50 51 51 from django.core.extensions import get_object_or_404, render_to_response … … 159 159 from django.conf.urls.defaults import * 160 160 161 urlpatterns = patterns('myproject.apps.polls.views .polls',161 urlpatterns = patterns('myproject.apps.polls.views', 162 162 (r'^$', 'index'), 163 163 (r'^(?P<poll_id>\d+)/$', 'detail'), … … 179 179 (r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict), 180 180 (r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='polls/results')), 181 (r'^(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views. polls.vote'),181 (r'^(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views.vote'), 182 182 ) 183 183 … … 218 218 219 219 Finally, you can delete the ``index()``, ``detail()`` and ``results()`` views 220 from ``polls/views /polls.py``. We don't need them anymore.220 from ``polls/views.py``. We don't need them anymore. 221 221 222 222 For full details on generic views, see the `generic views documentation`_.
