Django

Code

Changeset 1258

Show
Ignore:
Timestamp:
11/15/05 20:00:23 (3 years ago)
Author:
adrian
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/tutorial01.txt

    r948 r1258  
    132132            __init__.py 
    133133            polls.py 
    134         views/ 
    135             __init__.py 
     134        views.py 
    136135 
    137136This directory structure will house the poll application. 
  • django/trunk/docs/tutorial03.txt

    r1210 r1258  
    7474 
    7575    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'), 
    8080    ) 
    8181 
     
    8585and traverses the regular expressions in order. When it finds a regular 
    8686expression that matches -- ``r'^polls/(?P<poll_id>\d+)/$'`` -- it loads the 
    87 associated Python package/module: ``myproject.apps.polls.views.polls.detail``. That 
    88 corresponds to the function ``detail()`` in ``myproject/apps/polls/views/polls.py``. 
     87associated Python package/module: ``myproject.apps.polls.views.detail``. That 
     88corresponds to the function ``detail()`` in ``myproject/apps/polls/views.py``. 
    8989Finally, it calls that ``detail()`` function like so:: 
    9090 
     
    100100something like this:: 
    101101 
    102     (r'^polls/latest\.php$', 'myproject.apps.polls.views.polls.index'), 
    103  
    104 But, don't do that. It's stupid
     102    (r'^polls/latest\.php$', 'myproject.apps.polls.views.index'), 
     103 
     104But, don't do that. It's silly
    105105 
    106106If you need help with regular expressions, see `Wikipedia's entry`_ and the 
     
    126126 
    127127Now 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 
     128You 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 
     135This error happened because you haven't written a function ``index()`` in the 
     136module ``myproject/apps/polls/views.py``. 
    132137 
    133138Try "/polls/23/", "/polls/23/results/" and "/polls/23/vote/". The error 
    134 messages should tell you which view Django tried (and failed to find, because 
    135 you haven't written any views yet). 
    136  
    137 Time to write the first view. Create the file ``myproject/apps/polls/views/polls.py`` 
     139messages tell you which view Django tried (and failed to find, because you 
     140haven't written any views yet). 
     141 
     142Time to write the first view. Open the file ``myproject/apps/polls/views.py`` 
    138143and put the following Python code in it:: 
    139144 
     
    375380 
    376381    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'), 
    381386    ) 
    382387 
    383 Namely, ``myproject.apps.polls.views.polls`` is in every callback. 
     388Namely, ``myproject.apps.polls.views`` is in every callback. 
    384389 
    385390Because this is a common case, the URLconf framework provides a shortcut for 
     
    387392first argument to ``patterns()``, like so:: 
    388393 
    389     urlpatterns = patterns('myproject.apps.polls.views.polls', 
     394    urlpatterns = patterns('myproject.apps.polls.views', 
    390395        (r'^polls/$', 'index'), 
    391396        (r'^polls/(?P<poll_id>\d+)/$', 'detail'), 
     
    436441line:: 
    437442 
    438     urlpatterns = patterns('myproject.apps.polls.views.polls', 
     443    urlpatterns = patterns('myproject.apps.polls.views', 
    439444        (r'^$', 'index'), 
    440445        (r'^(?P<poll_id>\d+)/$', 'detail'), 
  • django/trunk/docs/tutorial04.txt

    r1034 r1258  
    4545included this line:: 
    4646 
    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 
     49So let's create a ``vote()`` function in ``myproject/apps/polls/views.py``:: 
    5050 
    5151    from django.core.extensions import get_object_or_404, render_to_response 
     
    159159    from django.conf.urls.defaults import * 
    160160 
    161     urlpatterns = patterns('myproject.apps.polls.views.polls', 
     161    urlpatterns = patterns('myproject.apps.polls.views', 
    162162        (r'^$', 'index'), 
    163163        (r'^(?P<poll_id>\d+)/$', 'detail'), 
     
    179179        (r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict), 
    180180        (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'), 
    182182    ) 
    183183 
     
    218218 
    219219Finally, you can delete the ``index()``, ``detail()`` and ``results()`` views 
    220 from ``polls/views/polls.py``. We don't need them anymore. 
     220from ``polls/views.py``. We don't need them anymore. 
    221221 
    222222For full details on generic views, see the `generic views documentation`_.