Django

Code

Changeset 1901

Show
Ignore:
Timestamp:
01/10/06 20:06:27 (3 years ago)
Author:
adrian
Message:

Updated tutorials 1-4 to use manage.py instead of django-admin.py, new directory layout (no /apps/ subdirectory) and other various tweaks

Files:

Legend:

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

    r1898 r1901  
    77 
    88The ``django-admin.py`` script should be on your system path if you installed 
    9 Django via its setup.py utility. If it's not on your path, you can find it in 
     9Django via its ``setup.py`` utility. If it's not on your path, you can find it in 
    1010``site-packages/django/bin`` within your Python installation. Consider 
    1111symlinking to it from some place on your path, such as ``/usr/local/bin``. 
    1212 
     13In addition, ``manage.py`` is automatically created in each Django project. 
     14``manage.py`` is a thin wrapper around ``django-admin.py`` that takes care of 
     15two things for you before delegating to ``django-admin.py``:: 
     16 
     17    * It puts your project's package on ``sys.path``. 
     18 
     19    * It sets the ``DJANGO_SETTINGS_MODULE`` environment variable so that it 
     20      points to your project's ``settings.py`` file. 
     21 
     22Generally, when working on a single Django project, it's easier to use 
     23``manage.py``. Use ``django-admin.py`` with ``DJANGO_SETTINGS_MODULE``, or the 
     24``--settings`` command line option, if you need to switch between multiple 
     25Django settings files. 
     26 
    1327Usage 
    1428===== 
    1529 
    1630``django-admin.py action [options]`` 
     31``manage.py action [options]`` 
    1732 
    1833``action`` should be one of the actions listed in this document. ``options``, 
     
    211226``django-admin.py`` will use the DJANGO_SETTINGS_MODULE environment variable. 
    212227 
     228Note that this option is unnecessary in ``manage.py``, because it takes care of 
     229setting ``DJANGO_SETTINGS_MODULE`` for you. 
     230 
    213231--pythonpath 
    214232------------ 
     
    222240variable. 
    223241 
     242Note that this option is unnecessary in ``manage.py``, because it takes care of 
     243setting the Python path for you. 
     244 
    224245.. _import search path: http://diveintopython.org/getting_to_know_python/everything_is_an_object.html 
    225246 
  • django/trunk/docs/tutorial01.txt

    r1676 r1901  
    3535.. admonition:: Where should this code live? 
    3636 
    37    If your background is in PHP, you're probably used to putting code under the 
    38    Web server's document root (in a place such as ``/var/www``). With Django, 
    39    you don't do that. It's not a good idea to put any of this Python code within 
    40    your Web server's document root, because it risks the possibility that 
    41    people may be able to view your code over the Web. That's not good for 
    42    security. 
    43  
    44    Put your code in some directory **outside** of the document root, such as 
    45    ``/home/mycode``. 
     37    If your background is in PHP, you're probably used to putting code under the 
     38    Web server's document root (in a place such as ``/var/www``). With Django, 
     39    you don't do that. It's not a good idea to put any of this Python code within 
     40    your Web server's document root, because it risks the possibility that 
     41    people may be able to view your code over the Web. That's not good for 
     42    security. 
     43 
     44    Put your code in some directory **outside** of the document root, such as 
     45    ``/home/mycode``. 
    4646 
    4747A project is a collection of settings for an instance of Django -- including 
     
    5151    myproject/ 
    5252        __init__.py 
    53         apps/ 
    54             __init__.py 
     53        manage.py 
    5554        settings.py 
    5655        urls.py 
    5756 
    58 First, edit ``myproject/settings.py``. It's a normal Python module with 
    59 module-level variables representing Django settings. Edit the file and change 
    60 these settings to match your database's connection parameters: 
     57These files are: 
     58 
     59    * ``manage.py``: A command-line utility that lets you interact with this 
     60      Django project in various ways. 
     61    * ``settings.py``: Settings/configuration for this Django project. 
     62    * ``urls.py``: The URL declarations for this Django project; a "table of 
     63      contents" of your Django-powered site. 
     64 
     65The development server 
     66---------------------- 
     67 
     68Change into the ``myproject`` directory, if you haven't already, and run the 
     69command ``python manage.py runserver``. You'll see the following output on the 
     70command line:: 
     71 
     72    Validating models... 
     73    0 errors found. 
     74 
     75    Starting server on port 8000 with settings module 'myproject.settings'. 
     76    Go to http://127.0.0.1:8000/ for Django. 
     77    Quit the server with CONTROL-C (Unix) or CTRL-BREAK (Windows). 
     78 
     79You've started the Django development server, a lightweight, pure-Python Web 
     80server that builds on the BaseHTTPServer included in Python's standard library. 
     81We've included this with Django so you can develop things rapidly, without 
     82having to deal with configuring Apache until you're ready for production. 
     83 
     84DON'T use this server in anything resembling a production environment. It's 
     85intended only for use while developing. 
     86 
     87.. admonition:: Changing the port 
     88 
     89    By default, the ``runserver`` command starts the development server on port 
     90    8000. If you want to change the server's port, pass it as a command-line 
     91    argument:: 
     92 
     93        python manage.py runserver 8080 
     94 
     95Now that the server's running, visit http://127.0.0.1:8000/ with your Web 
     96browser. You'll see a "Welcome to Django" page, in pleasant, light-blue pastel. 
     97It worked! 
     98 
     99Database setup 
     100-------------- 
     101 
     102Now, edit ``settings.py``. It's a normal Python module with module-level 
     103variables representing Django settings. Change these settings to match your 
     104database's connection parameters: 
    61105 
    62106    * ``DATABASE_ENGINE`` -- Either 'postgresql', 'mysql' or 'sqlite3'. 
    63107      More coming soon. 
    64108    * ``DATABASE_NAME`` -- The name of your database, or the full (absolute) 
    65       path to the database file if you're using sqlite. 
    66     * ``DATABASE_USER`` -- Your database username (not used for sqlite). 
    67     * ``DATABASE_PASSWORD`` -- Your database password (not used for sqlite). 
     109      path to the database file if you're using SQLite. 
     110    * ``DATABASE_USER`` -- Your database username (not used for SQLite). 
     111    * ``DATABASE_PASSWORD`` -- Your database password (not used for SQLite). 
    68112    * ``DATABASE_HOST`` -- The host your database is on. Leave this as an 
    69113      empty string if your database server is on the same physical machine 
    70       (not used for sqlite). 
     114      (not used for SQLite). 
    71115 
    72116.. admonition:: Note 
     
    76120    database's interactive prompt. 
    77121 
    78 Now, take a second to make sure ``myproject`` is on your Python path. You 
    79 can do this by copying ``myproject`` to Python's ``site-packages`` directory, 
    80 or you can do it by altering the ``PYTHONPATH`` environment variable. See the 
    81 `Python path documentation`_ for more information. If you opt to set the 
    82 ``PYTHONPATH`` environment variable, note that you'll need to set it to the 
    83 *parent* directory of ``myproject``. (You can test this by typing 
    84 "import myproject" into the Python interactive prompt.) 
    85  
    86 Run the following command:: 
    87  
    88     django-admin.py init --settings=myproject.settings 
    89  
    90 The ``django-admin.py`` utility generally needs to know which settings module 
    91 you're using. Here, we're doing that by specifying ``settings=`` on the command 
    92 line, but that can get tedious. If you don't want to type ``settings=`` each 
    93 time, you can set the ``DJANGO_SETTINGS_MODULE`` environment variable. Here's 
    94 how you do that in the Bash shell on Unix:: 
    95  
    96     export DJANGO_SETTINGS_MODULE=myproject.settings 
    97  
    98 On Windows, you'd use ``set`` instead:: 
    99  
    100     set DJANGO_SETTINGS_MODULE=myproject.settings 
    101  
    102 If you don't see any errors after running ``django-admin.py init``, you know it 
    103 worked. That command initialized your database with Django's core database 
    104 tables. If you're interested, run the command-line client for your database and 
    105 type ``\dt`` (PostgreSQL), ``SHOW TABLES;`` (MySQL), or ``.schema`` (SQLite) to 
    106 display the tables. 
    107  
    108 .. _`Python path documentation`: http://docs.python.org/tut/node8.html#SECTION008110000000000000000 
     122Run the following command to initialize your database with Django's core 
     123database tables:: 
     124 
     125    python manage.py init 
     126 
     127If you don't see any errors, it worked. 
     128 
     129If you're interested, run the command-line client for your database and type 
     130``\dt`` (PostgreSQL), ``SHOW TABLES;`` (MySQL), or ``.schema`` (SQLite) to 
     131display the tables Django created. 
     132 
     133.. admonition:: About those database tables 
     134 
     135    The tables created by ``manage.py init`` are for sessions, authentication 
     136    and other features Django provides. The next release of Django will have 
     137    a "lite" version of the ``init`` command that won't install any database 
     138    tables if you don't want them. 
    109139 
    110140Creating models 
     
    112142 
    113143Now that your environment -- a "project" -- is set up, you're set to start 
    114 doing work. (You won't have to take care of this boring administrative stuff 
     144doing work. (You won't have to take care of that boring administrative stuff 
    115145again.) 
    116146 
    117 Each application you write in Django -- e.g., a weblog system, a database of 
    118 public records or a simple poll app -- consists of a Python package, somewhere 
    119 on your Python path, that follows a certain convention. Django comes with a 
     147Each application you write in Django consists of a Python package, somewhere 
     148on your `Python path`_, that follows a certain convention. Django comes with a 
    120149utility that automatically generates the basic directory structure of an app, 
    121150so you can focus on writing code rather than creating directories. 
    122151 
    123 In this tutorial, we'll create our poll app in the ``myproject/apps`` 
    124 directory, for simplicity. As a consequence, the app will be coupled to the 
    125 project -- that is, Python code within the poll app will refer to 
    126 ``myproject.apps.polls``. Later in this tutorial, we'll discuss decoupling 
    127 your apps for distribution. 
    128  
    129 To create your app, change into the ``myproject/apps`` directory and type this 
    130 command:: 
    131  
    132     django-admin.py startapp polls 
    133  
    134 (From now on, this tutorial will leave out the ``--settings`` parameter and 
    135 will assume you've either set your ``DJANGO_SETTINGS_MODULE`` environment 
    136 variable or included the ``--settings`` option in your call to the command.) 
    137  
    138 That'll create a directory structure like this:: 
     152.. admonition:: Projects vs. apps 
     153 
     154    What's the difference between a project and an app? An app is a Web 
     155    application that does something -- e.g., a weblog system, a database of 
     156    public records or a simple poll app. A project is a collection of 
     157    configuration and apps for a particular Web site. A project can contain 
     158    multiple apps. An app can be in multiple projects. 
     159 
     160In this tutorial, we'll create our poll app in the ``myproject`` directory, 
     161for simplicity. As a consequence, the app will be coupled to the project -- 
     162that is, Python code within the poll app will refer to ``myproject.polls``. 
     163Later in this tutorial, we'll discuss decoupling your apps for distribution. 
     164 
     165To create your app, make sure you're in the ``myproject`` directory and type 
     166this command:: 
     167 
     168    python manage.py startapp polls 
     169 
     170That'll create a directory ``polls``, which is laid out like this:: 
    139171 
    140172    polls/ 
     
    202234database relationships: many-to-ones, many-to-manys and one-to-ones. 
    203235 
     236.. _`Python path`: http://docs.python.org/tut/node8.html#SECTION008110000000000000000 
    204237.. _DRY Principle: http://c2.com/cgi/wiki?DontRepeatYourself 
    205238 
     
    210243is able to: 
    211244 
    212 * Create a database schema (``CREATE TABLE`` statements) for this app. 
    213 * Create a Python database-access API for accessing Poll and Choice objects. 
     245    * Create a database schema (``CREATE TABLE`` statements) for this app. 
     246    * Create a Python database-access API for accessing Poll and Choice objects. 
    214247 
    215248But first we need to tell our project that the ``polls`` app is installed. 
     
    217250.. admonition:: Philosophy 
    218251 
    219    Django apps are "pluggable": You can use an app in multiple 
    220    projects, and you can distribute apps, because they don't have to be tied to 
    221    a given Django installation. 
    222  
    223 Edit the myproject/settings.py file again, and change the ``INSTALLED_APPS`` 
    224 setting to include the string "myproject.apps.polls". So it'll look like this:: 
     252    Django apps are "pluggable": You can use an app in multiple projects, and 
     253    you can distribute apps, because they don't have to be tied to a given 
     254    Django installation. 
     255 
     256Edit the ``settings.py`` file again, and change the ``INSTALLED_APPS`` setting 
     257to include the string ``'myproject.polls'``. So it'll look like this:: 
    225258 
    226259    INSTALLED_APPS = ( 
    227         'myproject.apps.polls', 
     260        'myproject.polls', 
    228261    ) 
    229262 
    230 (Don't forget the trailing comma because of Python's rules about single-value 
    231 tuples.) 
    232  
    233 Now Django knows myproject includes the polls app. Let's run another command:: 
    234  
    235     django-admin.py sql polls 
    236  
    237 (Note that it doesn't matter which directory you're in when you run this command.) 
     263(Don't forget the trailing comma, because of Python's rule about single-value 
     264tuples: Without a trailing comma, Python wouldn't know this was a tuple.) 
     265 
     266Now Django knows ``myproject`` includes the ``polls`` app. Let's run another command:: 
     267 
     268    python manage.py sql polls 
    238269 
    239270You should see the following (the CREATE TABLE SQL statements for the polls app):: 
     
    256287 
    257288    * Table names are automatically generated by combining the name of the app 
    258       (polls) with a plural version of the object name (polls and choices). (You 
    259       can override this behavior.) 
     289      (``polls``) with a plural version of the object name (polls and choices). 
     290      (You can override this behavior.) 
    260291 
    261292    * Primary keys (IDs) are added automatically. (You can override this, too.) 
     
    266297    * The foreign key relationship is made explicit by a ``REFERENCES`` statement. 
    267298 
    268     * It's tailored to the database you're using, so database-specific field types 
    269       such as ``auto_increment`` (MySQL), ``serial`` (PostgreSQL), or ``intege
    270       primary key`` (SQLite) are handled for you automatically. Same goes for 
    271       quoting of field names -- e.g., using double quotes or single quotes. Th
    272       author of this tutorial runs PostgreSQL, so the example output is in 
    273       PostgreSQL syntax. 
     299    * It's tailored to the database you're using, so database-specific field 
     300      types such as ``auto_increment`` (MySQL), ``serial`` (PostgreSQL), o
     301      ``integer primary key`` (SQLite) are handled for you automatically. Same 
     302      goes for quoting of field names -- e.g., using double quotes or singl
     303      quotes. The author of this tutorial runs PostgreSQL, so the example 
     304      output is inPostgreSQL syntax. 
    274305 
    275306If you're interested, also run the following commands: 
    276307 
    277     * ``django-admin.py sqlinitialdata polls`` -- Outputs the initial-data 
     308    * ``python manage.py sqlinitialdata polls`` -- Outputs the initial-data 
    278309      inserts required for Django's admin framework. 
    279310 
    280     * ``django-admin.py sqlclear polls`` -- Outputs the necessary ``DROP 
     311    * ``python manage.py sqlclear polls`` -- Outputs the necessary ``DROP 
    281312      TABLE`` statements for this app, according to which tables already exist 
    282313      in your database (if any). 
    283314 
    284     * ``django-admin.py sqlindexes polls`` -- Outputs the ``CREATE INDEX`` 
     315    * ``python manage.py sqlindexes polls`` -- Outputs the ``CREATE INDEX`` 
    285316      statements for this app. 
    286317 
    287     * ``django-admin.py sqlall polls`` -- A combination of 'sql' and 
     318    * ``python manage.py sqlall polls`` -- A combination of 'sql' and 
    288319      'sqlinitialdata'. 
    289320 
     
    294325automatically:: 
    295326 
    296     django-admin.py install polls 
     327    python manage.py install polls 
    297328 
    298329Behind the scenes, all that command does is take the output of 
    299 ``django-admin.py sqlall polls`` and execute it in the database pointed-to by 
     330``python manage.py sqlall polls`` and execute it in the database pointed-to by 
    300331your Django settings file. 
    301332 
    302 Read the `django-admin.py documentation`_ for full information on what this 
    303 utility can do. 
     333Read the `django-admin.py documentation`_ for full information on what the 
     334``manage.py`` utility can do. 
    304335 
    305336.. _django-admin.py documentation: http://www.djangoproject.com/documentation/django_admin/ 
     
    308339==================== 
    309340 
    310 Now, make sure your DJANGO_SETTINGS_MODULE environment variable is set (as 
    311 explained above), and open the Python interactive shell to play around with the 
    312 free Python API Django gives you:: 
     341Now, let's hop into the interactive Python shell and play around with the free 
     342API Django gives you. To invoke the Python shell, use this command:: 
     343 
     344    python manage.py shell 
     345 
     346We're using this instead of simply typing "python", because ``manage.py`` sets 
     347up the project's environment for you. "Setting up the environment" involves two 
     348things: 
     349 
     350    * Putting ``myproject`` on ``sys.path``. For flexibility, several pieces of 
     351      Django refer to projects in Python dotted-path notation (e.g. 
     352      ``'myproject.polls.models'``). In order for this to work, the 
     353      ``myproject`` package has to be on ``sys.path``. 
     354 
     355      We've already seen one example of this: the ``INSTALLED_APPS`` setting is 
     356      a list of packages in dotted-path notation. 
     357 
     358    * Setting the ``DJANGO_SETTINGS_MODULE`` environment variable, which gives 
     359      Django the path to your ``settings.py`` file. 
     360 
     361.. admonition:: Bypassing manage.py 
     362 
     363    If you'd rather not use ``manage.py``, no problem. Just make sure 
     364    ``myproject`` is at the root level on the Python path (i.e., 
     365    ``import myproject`` works) and set the ``DJANGO_SETTINGS_MODULE`` 
     366    environment variable to ``myproject.settings``. 
     367 
     368    For more information on all of this, see the `django-admin.py documentation`_. 
     369 
     370Once you're in the shell, explore the database API:: 
    313371 
    314372    # Modules are dynamically created within django.models. 
     
    327385    >>> p.save() 
    328386 
    329     # Now it has an ID. 
     387    # Now it has an ID. Note that this might say "1L" instead of "1", depending 
     388    # on which database you're using. That's no biggie; it just means your 
     389    # database backend prefers to return integers as Python long integer 
     390    # objects. 
    330391    >>> p.id 
    331392    1 
     
    376437``datetime`` module from the Python standard library. 
    377438 
    378 Let's jump back into the Python interactive shell:: 
     439Let's jump back into the Python interactive shell by running 
     440``python manage.py shell`` again:: 
    379441 
    380442    >>> from django.models.polls import polls, choices 
  • django/trunk/docs/tutorial02.txt

    r1300 r1901  
    3232 
    3333    * Add ``"django.contrib.admin"`` to your ``INSTALLED_APPS`` setting. 
    34     * Run the command ``django-admin.py install admin``. This will create an 
     34    * Run the command ``python manage.py install admin``. This will create an 
    3535      extra database table that the admin needs. 
    3636    * Edit your ``myproject/urls.py`` file and uncomment the line below 
     
    4444Run the following command to create a superuser account for your admin site:: 
    4545 
    46     django-admin.py createsuperuser --settings=myproject.settings 
     46    python manage.py createsuperuser 
    4747 
    4848The script will prompt you for a username, e-mail address and password (twice). 
     
    5151============================ 
    5252 
    53 To make things easy, Django comes with a pure-Python Web server that builds on 
    54 the BaseHTTPServer included in Python's standard library. Let's start the 
    55 server and explore the admin site. 
    56  
    57 Just run the following command to start the server:: 
    58  
    59     django-admin.py runserver --settings=myproject.settings 
    60  
    61 It'll start a Web server running locally -- on port 8000, by default. If you 
    62 want to change the server's port, pass it as a command-line argument:: 
    63  
    64     django-admin.py runserver 8080 --settings=myproject.settings 
    65  
    66 DON'T use this server in anything resembling a production environment. It's 
    67 intended only for use while developing. 
     53Let's start the development server and explore the admin site. 
     54 
     55Recall from Tutorial 1 that you start the development server like so:: 
     56 
     57    python manage.py runserver 
    6858 
    6959Now, open a Web browser and go to "/admin/" on your local domain -- e.g., 
     
    9282But where's our poll app? It's not displayed on the admin index page. 
    9383 
    94 Just one thing to do: We need to specify in the ``polls.Poll`` model that Poll 
    95 objects have an admin interface. Edit the ``myproject/apps/polls/models/polls.py`` 
     84Just one thing to do: We need to specify in the ``Poll`` model that ``Poll`` 
     85objects have an admin interface. Edit the ``myproject/polls/models/polls.py`` 
    9686file and make the following change to add an inner ``META`` class with an 
    9787``admin`` attribute:: 
     
    10292            admin = meta.Admin() 
    10393 
    104 The ``class META`` contains all non-field metadata about this model. 
     94The ``class META`` contains all `non-field metadata`_ about this model. 
    10595 
    10696Now reload the Django admin page to see your changes. Note that you don't have 
    10797to restart the development server -- it auto-reloads code. 
     98 
     99.. _non-field metadata: http://www.djangoproject.com/documentation/model_api/#meta-options 
    108100 
    109101Explore the free admin functionality 
     
    217209====================== 
    218210 
    219 OK, we have our Poll admin page. But a ``Poll`` has multiple ``Choices``, and the admin 
    220 page doesn't display choices. 
     211OK, we have our Poll admin page. But a ``Poll`` has multiple ``Choices``, and 
     212the admin page doesn't display choices. 
    221213 
    222214Yet. 
    223215 
    224 In this case, there are two ways to solve this problem. The first is to give 
    225 the ``Choice`` model its own ``admin`` attribute, just as we did with ``Poll``. 
    226 Here's what that would look like:: 
     216There are two ways to solve this problem. The first is to give the ``Choice`` 
     217model its own ``admin`` attribute, just as we did with ``Poll``. Here's what 
     218that would look like:: 
    227219 
    228220    class Choice(meta.Model): 
     
    238230 
    239231In that form, the "Poll" field is a select box containing every poll in the 
    240 database. In our case, only one poll exists at this point. 
     232database. Django knows that a ``ForeignKey`` should be represented in the admin 
     233as a ``<select>`` box. In our case, only one poll exists at this point. 
    241234 
    242235Also note the "Add Another" link next to "Poll." Every object with a ForeignKey 
     
    364357That adds a search box at the top of the change list. When somebody enters 
    365358search terms, Django will search the ``question`` field. You can use as many 
    366 fields as you'd like -- although because it uses a LIKE query behind the 
     359fields as you'd like -- although because it uses a ``LIKE`` query behind the 
    367360scenes, keep it reasonable, to keep your database happy. 
    368361 
     
    445438 
    446439Django offers another shortcut in this department. Run the command 
    447 ``django-admin.py adminindex polls`` to get a chunk of template code for 
     440``python manage.py adminindex polls`` to get a chunk of template code for 
    448441inclusion in the admin index template. It's a useful starting point. 
    449442 
  • django/trunk/docs/tutorial03.txt

    r1291 r1901  
    6363For more details on URLconfs, see the `URLconf documentation`_. 
    6464 
    65 When you ran ``django-admin.py startproject myproject`` at the beginning of 
     65When you ran ``python manage.py startproject myproject`` at the beginning of 
    6666Tutorial 1, it created a default URLconf in ``myproject/urls.py``. It also 
    6767automatically set your ``ROOT_URLCONF`` setting to point at that file:: 
     
    7474 
    7575    urlpatterns = patterns('', 
    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'), 
     76        (r'^polls/$', 'myproject.polls.views.index'), 
     77        (r'^polls/(\d+)/$', 'myproject.polls.views.detail'), 
     78        (r'^polls/(\d+)/results/$', 'myproject.polls.views.results'), 
     79        (r'^polls/(\d+)/vote/$', 'myproject.polls.views.vote'), 
    8080    ) 
    8181 
     
    8484by the ``ROOT_URLCONF`` setting. It finds the variable named ``urlpatterns`` 
    8585and traverses the regular expressions in order. When it finds a regular 
    86 expression that matches -- ``r'^polls/(?P<poll_id>\d+)/$'`` -- it loads the 
    87 associated Python package/module: ``myproject.apps.polls.views.detail``. That 
    88 corresponds to the function ``detail()`` in ``myproject/apps/polls/views.py``. 
     86expression that matches -- ``r'^polls/(\d+)/$'`` -- it loads the 
     87associated Python package/module: ``myproject.polls.views.detail``. That 
     88corresponds to the function ``detail()`` in ``myproject/polls/views.py``. 
    8989Finally, it calls that ``detail()`` function like so:: 
    9090 
    9191    detail(request=<HttpRequest object>, poll_id='23') 
    9292 
    93 The ``poll_id='23'`` part comes from ``(?P<poll_id>\d+)``. Using 
    94 ``(?P<name>pattern)`` "captures" the text matched by ``pattern`` and sends i
    95 as a keyword argument to the view function. 
     93The ``poll_id='23'`` part comes from ``(\d+)``. Using parenthesis around a 
     94pattern "captures" the text matched by that pattern and sends it as an argumen
     95to the view function. 
    9696 
    9797Because the URL patterns are regular expressions, there really is no limit on 
     
    100100something like this:: 
    101101 
    102     (r'^polls/latest\.php$', 'myproject.apps.polls.views.index'), 
     102    (r'^polls/latest\.php$', 'myproject.polls.views.index'), 
    103103 
    104104But, don't do that. It's silly. 
     
    129129Fire up the Django development Web server:: 
    130130 
    131     django-admin.py runserver --settings=myproject.settings 
     131    python manage.py runserver 
    132132 
    133133Now go to "http://localhost:8000/polls/" on your domain in your Web browser. 
     
    136136    ViewDoesNotExist at /polls/ 
    137137 
    138     Tried index in module myproject.apps.polls.views. Error was: 'module' 
     138    Tried index in module myproject.polls.views. Error was: 'module' 
    139139    object has no attribute 'index' 
    140140 
    141141This error happened because you haven't written a function ``index()`` in the 
    142 module ``myproject/apps/polls/views.py``. 
     142module ``myproject/polls/views.py``. 
    143143 
    144144Try "/polls/23/", "/polls/23/results/" and "/polls/23/vote/". The error 
     
    146146haven't written any views yet). 
    147147 
    148 Time to write the first view. Open the file ``myproject/apps/polls/views.py`` 
     148Time to write the first view. Open the file ``myproject/polls/views.py`` 
    149149and put the following Python code in it:: 
    150150 
     
    223223probably shouldn't make them public, just for security's sake. 
    224224 
    225 Then edit ``TEMPLATE_DIRS`` in your settings file (``settings.py``) to tell 
    226 Django where it can find templates -- just as you did in the "Customize the 
    227 admin look and feel" section of Tutorial 2. 
     225Then edit ``TEMPLATE_DIRS`` in your ``settings.py`` to tell Django where it can 
     226find templates -- just as you did in the "Customize the admin look and feel" 
     227section of Tutorial 2. 
    228228 
    229229When you've done that, create a directory ``polls`` in your template directory. 
     
    386386 
    387387    urlpatterns = patterns('', 
    388         (r'^polls/$', 'myproject.apps.polls.views.index'), 
    389         (r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views.detail'), 
    390         (r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.apps.polls.views.results'), 
    391         (r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.apps.polls.views.vote'), 
     388        (r'^polls/$', 'myproject.polls.views.index'), 
     389        (r'^polls/(?P<poll_id>\d+)/$', 'myproject.polls.views.detail'), 
     390        (r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.polls.views.results'), 
     391        (r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.polls.views.vote'), 
    392392    ) 
    393393 
    394 Namely, ``myproject.apps.polls.views`` is in every callback. 
     394Namely, ``myproject.polls.views`` is in every callback. 
    395395 
    396396Because this is a common case, the URLconf framework provides a shortcut for 
     
    398398first argument to ``patterns()``, like so:: 
    399399 
    400     urlpatterns = patterns('myproject.apps.polls.views', 
     400    urlpatterns = patterns('myproject.polls.views', 
    401401        (r'^polls/$', 'index'), 
    402402        (r'^polls/(?P<poll_id>\d+)/$', 'detail'), 
     
    417417 
    418418Our poll app is pretty decoupled at this point, thanks to the strict directory 
    419 structure that ``django-admin.py startapp`` created, but one part of it is 
     419structure that ``python manage.py startapp`` created, but one part of it is 
    420420coupled to the Django settings: The URLconf. 
    421421 
     
    424424URLs within the app directory. 
    425425 
    426 Copy the file ``myproject/urls.py`` to ``myproject/apps/polls/urls.py``. Then, 
     426Copy the file ``myproject/urls.py`` to ``myproject/polls/urls.py``. Then, 
    427427change ``myproject/urls.py`` to remove the poll-specific URLs and insert an 
    428428``include()``:: 
    429429 
    430     (r'^polls/', include('myproject.apps.polls.urls')), 
     430    (r'^polls/', include('myproject.polls.urls')), 
    431431 
    432432``include()``, simply, references another URLconf. Note that the regular 
     
    440440* Django will find the match at ``'^polls/'`` 
    441441* It will strip off the matching text (``"polls/"``) and send the remaining 
    442   text -- ``"34/"`` -- to the 'myproject.apps.polls.urls' urlconf for 
     442  text -- ``"34/"`` -- to the 'myproject.polls.urls' urlconf for 
    443443  further processing. 
    444444 
    445445Now that we've decoupled that, we need to decouple the 
    446 'myproject.apps.polls.urls' urlconf by removing the leading "polls/" from each 
     446'myproject.polls.urls' urlconf by removing the leading "polls/" from each 
    447447line:: 
    448448 
    449     urlpatterns = patterns('myproject.apps.polls.views', 
     449    urlpatterns = patterns('myproject.polls.views', 
    450450        (r'^$', 'index'), 
    451451        (r'^(?P<poll_id>\d+)/$', 'detail'), 
  • django/trunk/docs/tutorial04.txt

    r1258 r1901  
    4545included this line:: 
    4646 
    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``:: 
     47    (r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.polls.views.vote'), 
     48 
     49So let's create a ``vote()`` function in ``myproject/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', 
     161    urlpatterns = patterns('myproject.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.vote'), 
     181        (r'^(?P<poll_id>\d+)/vote/$', 'myproject.polls.views.vote'), 
    182182    ) 
    183183