Changeset 1901
- Timestamp:
- 01/10/06 20:06:27 (3 years ago)
- Files:
-
- django/trunk/docs/django-admin.txt (modified) (3 diffs)
- django/trunk/docs/tutorial01.txt (modified) (13 diffs)
- django/trunk/docs/tutorial02.txt (modified) (9 diffs)
- django/trunk/docs/tutorial03.txt (modified) (13 diffs)
- django/trunk/docs/tutorial04.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/docs/django-admin.txt
r1898 r1901 7 7 8 8 The ``django-admin.py`` script should be on your system path if you installed 9 Django via its setup.pyutility. If it's not on your path, you can find it in9 Django via its ``setup.py`` utility. If it's not on your path, you can find it in 10 10 ``site-packages/django/bin`` within your Python installation. Consider 11 11 symlinking to it from some place on your path, such as ``/usr/local/bin``. 12 12 13 In 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 15 two 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 22 Generally, 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 25 Django settings files. 26 13 27 Usage 14 28 ===== 15 29 16 30 ``django-admin.py action [options]`` 31 ``manage.py action [options]`` 17 32 18 33 ``action`` should be one of the actions listed in this document. ``options``, … … 211 226 ``django-admin.py`` will use the DJANGO_SETTINGS_MODULE environment variable. 212 227 228 Note that this option is unnecessary in ``manage.py``, because it takes care of 229 setting ``DJANGO_SETTINGS_MODULE`` for you. 230 213 231 --pythonpath 214 232 ------------ … … 222 240 variable. 223 241 242 Note that this option is unnecessary in ``manage.py``, because it takes care of 243 setting the Python path for you. 244 224 245 .. _import search path: http://diveintopython.org/getting_to_know_python/everything_is_an_object.html 225 246 django/trunk/docs/tutorial01.txt
r1676 r1901 35 35 .. admonition:: Where should this code live? 36 36 37 If your background is in PHP, you're probably used to putting code under the38 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 within40 your Web server's document root, because it risks the possibility that41 people may be able to view your code over the Web. That's not good for42 security.43 44 Put your code in some directory **outside** of the document root, such as45 ``/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``. 46 46 47 47 A project is a collection of settings for an instance of Django -- including … … 51 51 myproject/ 52 52 __init__.py 53 apps/ 54 __init__.py 53 manage.py 55 54 settings.py 56 55 urls.py 57 56 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: 57 These 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 65 The development server 66 ---------------------- 67 68 Change into the ``myproject`` directory, if you haven't already, and run the 69 command ``python manage.py runserver``. You'll see the following output on the 70 command 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 79 You've started the Django development server, a lightweight, pure-Python Web 80 server that builds on the BaseHTTPServer included in Python's standard library. 81 We've included this with Django so you can develop things rapidly, without 82 having to deal with configuring Apache until you're ready for production. 83 84 DON'T use this server in anything resembling a production environment. It's 85 intended 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 95 Now that the server's running, visit http://127.0.0.1:8000/ with your Web 96 browser. You'll see a "Welcome to Django" page, in pleasant, light-blue pastel. 97 It worked! 98 99 Database setup 100 -------------- 101 102 Now, edit ``settings.py``. It's a normal Python module with module-level 103 variables representing Django settings. Change these settings to match your 104 database's connection parameters: 61 105 62 106 * ``DATABASE_ENGINE`` -- Either 'postgresql', 'mysql' or 'sqlite3'. 63 107 More coming soon. 64 108 * ``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). 68 112 * ``DATABASE_HOST`` -- The host your database is on. Leave this as an 69 113 empty string if your database server is on the same physical machine 70 (not used for sqlite).114 (not used for SQLite). 71 115 72 116 .. admonition:: Note … … 76 120 database's interactive prompt. 77 121 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 122 Run the following command to initialize your database with Django's core 123 database tables:: 124 125 python manage.py init 126 127 If you don't see any errors, it worked. 128 129 If you're interested, run the command-line client for your database and type 130 ``\dt`` (PostgreSQL), ``SHOW TABLES;`` (MySQL), or ``.schema`` (SQLite) to 131 display 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. 109 139 110 140 Creating models … … 112 142 113 143 Now that your environment -- a "project" -- is set up, you're set to start 114 doing work. (You won't have to take care of th isboring administrative stuff144 doing work. (You won't have to take care of that boring administrative stuff 115 145 again.) 116 146 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 147 Each application you write in Django consists of a Python package, somewhere 148 on your `Python path`_, that follows a certain convention. Django comes with a 120 149 utility that automatically generates the basic directory structure of an app, 121 150 so you can focus on writing code rather than creating directories. 122 151 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 160 In this tutorial, we'll create our poll app in the ``myproject`` directory, 161 for simplicity. As a consequence, the app will be coupled to the project -- 162 that is, Python code within the poll app will refer to ``myproject.polls``. 163 Later in this tutorial, we'll discuss decoupling your apps for distribution. 164 165 To create your app, make sure you're in the ``myproject`` directory and type 166 this command:: 167 168 python manage.py startapp polls 169 170 That'll create a directory ``polls``, which is laid out like this:: 139 171 140 172 polls/ … … 202 234 database relationships: many-to-ones, many-to-manys and one-to-ones. 203 235 236 .. _`Python path`: http://docs.python.org/tut/node8.html#SECTION008110000000000000000 204 237 .. _DRY Principle: http://c2.com/cgi/wiki?DontRepeatYourself 205 238 … … 210 243 is able to: 211 244 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. 214 247 215 248 But first we need to tell our project that the ``polls`` app is installed. … … 217 250 .. admonition:: Philosophy 218 251 219 Django apps are "pluggable": You can use an app in multiple220 projects, and you can distribute apps, because they don't have to be tied to221 a givenDjango 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 256 Edit the ``settings.py`` file again, and change the ``INSTALLED_APPS`` setting 257 to include the string ``'myproject.polls'``. So it'll look like this:: 225 258 226 259 INSTALLED_APPS = ( 227 'myproject. apps.polls',260 'myproject.polls', 228 261 ) 229 262 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 264 tuples: Without a trailing comma, Python wouldn't know this was a tuple.) 265 266 Now Django knows ``myproject`` includes the ``polls`` app. Let's run another command:: 267 268 python manage.py sql polls 238 269 239 270 You should see the following (the CREATE TABLE SQL statements for the polls app):: … … 256 287 257 288 * 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). (You259 can override this behavior.)289 (``polls``) with a plural version of the object name (polls and choices). 290 (You can override this behavior.) 260 291 261 292 * Primary keys (IDs) are added automatically. (You can override this, too.) … … 266 297 * The foreign key relationship is made explicit by a ``REFERENCES`` statement. 267 298 268 * It's tailored to the database you're using, so database-specific field types269 such as ``auto_increment`` (MySQL), ``serial`` (PostgreSQL), or ``integer270 primary key`` (SQLite) are handled for you automatically. Same goes for271 quoting of field names -- e.g., using double quotes or single quotes. The272 author of this tutorial runs PostgreSQL, so the example output is in273 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), or 301 ``integer primary key`` (SQLite) are handled for you automatically. Same 302 goes for quoting of field names -- e.g., using double quotes or single 303 quotes. The author of this tutorial runs PostgreSQL, so the example 304 output is inPostgreSQL syntax. 274 305 275 306 If you're interested, also run the following commands: 276 307 277 * `` django-admin.py sqlinitialdata polls`` -- Outputs the initial-data308 * ``python manage.py sqlinitialdata polls`` -- Outputs the initial-data 278 309 inserts required for Django's admin framework. 279 310 280 * `` django-admin.py sqlclear polls`` -- Outputs the necessary ``DROP311 * ``python manage.py sqlclear polls`` -- Outputs the necessary ``DROP 281 312 TABLE`` statements for this app, according to which tables already exist 282 313 in your database (if any). 283 314 284 * `` django-admin.py sqlindexes polls`` -- Outputs the ``CREATE INDEX``315 * ``python manage.py sqlindexes polls`` -- Outputs the ``CREATE INDEX`` 285 316 statements for this app. 286 317 287 * `` django-admin.py sqlall polls`` -- A combination of 'sql' and318 * ``python manage.py sqlall polls`` -- A combination of 'sql' and 288 319 'sqlinitialdata'. 289 320 … … 294 325 automatically:: 295 326 296 django-admin.py install polls327 python manage.py install polls 297 328 298 329 Behind 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 by330 ``python manage.py sqlall polls`` and execute it in the database pointed-to by 300 331 your Django settings file. 301 332 302 Read the `django-admin.py documentation`_ for full information on what th is303 utility can do.333 Read the `django-admin.py documentation`_ for full information on what the 334 ``manage.py`` utility can do. 304 335 305 336 .. _django-admin.py documentation: http://www.djangoproject.com/documentation/django_admin/ … … 308 339 ==================== 309 340 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:: 341 Now, let's hop into the interactive Python shell and play around with the free 342 API Django gives you. To invoke the Python shell, use this command:: 343 344 python manage.py shell 345 346 We're using this instead of simply typing "python", because ``manage.py`` sets 347 up the project's environment for you. "Setting up the environment" involves two 348 things: 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 370 Once you're in the shell, explore the database API:: 313 371 314 372 # Modules are dynamically created within django.models. … … 327 385 >>> p.save() 328 386 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. 330 391 >>> p.id 331 392 1 … … 376 437 ``datetime`` module from the Python standard library. 377 438 378 Let's jump back into the Python interactive shell:: 439 Let's jump back into the Python interactive shell by running 440 ``python manage.py shell`` again:: 379 441 380 442 >>> from django.models.polls import polls, choices django/trunk/docs/tutorial02.txt
r1300 r1901 32 32 33 33 * Add ``"django.contrib.admin"`` to your ``INSTALLED_APPS`` setting. 34 * Run the command `` django-admin.py install admin``. This will create an34 * Run the command ``python manage.py install admin``. This will create an 35 35 extra database table that the admin needs. 36 36 * Edit your ``myproject/urls.py`` file and uncomment the line below … … 44 44 Run the following command to create a superuser account for your admin site:: 45 45 46 django-admin.py createsuperuser --settings=myproject.settings46 python manage.py createsuperuser 47 47 48 48 The script will prompt you for a username, e-mail address and password (twice). … … 51 51 ============================ 52 52 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. 53 Let's start the development server and explore the admin site. 54 55 Recall from Tutorial 1 that you start the development server like so:: 56 57 python manage.py runserver 68 58 69 59 Now, open a Web browser and go to "/admin/" on your local domain -- e.g., … … 92 82 But where's our poll app? It's not displayed on the admin index page. 93 83 94 Just one thing to do: We need to specify in the `` polls.Poll`` model that Poll95 objects have an admin interface. Edit the ``myproject/ apps/polls/models/polls.py``84 Just one thing to do: We need to specify in the ``Poll`` model that ``Poll`` 85 objects have an admin interface. Edit the ``myproject/polls/models/polls.py`` 96 86 file and make the following change to add an inner ``META`` class with an 97 87 ``admin`` attribute:: … … 102 92 admin = meta.Admin() 103 93 104 The ``class META`` contains all non-field metadataabout this model.94 The ``class META`` contains all `non-field metadata`_ about this model. 105 95 106 96 Now reload the Django admin page to see your changes. Note that you don't have 107 97 to restart the development server -- it auto-reloads code. 98 99 .. _non-field metadata: http://www.djangoproject.com/documentation/model_api/#meta-options 108 100 109 101 Explore the free admin functionality … … 217 209 ====================== 218 210 219 OK, we have our Poll admin page. But a ``Poll`` has multiple ``Choices``, and the admin220 page doesn't display choices.211 OK, we have our Poll admin page. But a ``Poll`` has multiple ``Choices``, and 212 the admin page doesn't display choices. 221 213 222 214 Yet. 223 215 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 whatthat would look like::216 There are two ways to solve this problem. The first is to give the ``Choice`` 217 model its own ``admin`` attribute, just as we did with ``Poll``. Here's what 218 that would look like:: 227 219 228 220 class Choice(meta.Model): … … 238 230 239 231 In 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. 232 database. Django knows that a ``ForeignKey`` should be represented in the admin 233 as a ``<select>`` box. In our case, only one poll exists at this point. 241 234 242 235 Also note the "Add Another" link next to "Poll." Every object with a ForeignKey … … 364 357 That adds a search box at the top of the change list. When somebody enters 365 358 search terms, Django will search the ``question`` field. You can use as many 366 fields as you'd like -- although because it uses a LIKEquery behind the359 fields as you'd like -- although because it uses a ``LIKE`` query behind the 367 360 scenes, keep it reasonable, to keep your database happy. 368 361 … … 445 438 446 439 Django offers another shortcut in this department. Run the command 447 `` django-admin.py adminindex polls`` to get a chunk of template code for440 ``python manage.py adminindex polls`` to get a chunk of template code for 448 441 inclusion in the admin index template. It's a useful starting point. 449 442 django/trunk/docs/tutorial03.txt
r1291 r1901 63 63 For more details on URLconfs, see the `URLconf documentation`_. 64 64 65 When you ran `` django-admin.py startproject myproject`` at the beginning of65 When you ran ``python manage.py startproject myproject`` at the beginning of 66 66 Tutorial 1, it created a default URLconf in ``myproject/urls.py``. It also 67 67 automatically set your ``ROOT_URLCONF`` setting to point at that file:: … … 74 74 75 75 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'), 80 80 ) 81 81 … … 84 84 by the ``ROOT_URLCONF`` setting. It finds the variable named ``urlpatterns`` 85 85 and traverses the regular expressions in order. When it finds a regular 86 expression that matches -- ``r'^polls/( ?P<poll_id>\d+)/$'`` -- it loads the87 associated Python package/module: ``myproject. apps.polls.views.detail``. That88 corresponds to the function ``detail()`` in ``myproject/ apps/polls/views.py``.86 expression that matches -- ``r'^polls/(\d+)/$'`` -- it loads the 87 associated Python package/module: ``myproject.polls.views.detail``. That 88 corresponds to the function ``detail()`` in ``myproject/polls/views.py``. 89 89 Finally, it calls that ``detail()`` function like so:: 90 90 91 91 detail(request=<HttpRequest object>, poll_id='23') 92 92 93 The ``poll_id='23'`` part comes from ``( ?P<poll_id>\d+)``. Using94 ``(?P<name>pattern)`` "captures" the text matched by ``pattern`` and sends it95 as a keyword argumentto the view function.93 The ``poll_id='23'`` part comes from ``(\d+)``. Using parenthesis around a 94 pattern "captures" the text matched by that pattern and sends it as an argument 95 to the view function. 96 96 97 97 Because the URL patterns are regular expressions, there really is no limit on … … 100 100 something like this:: 101 101 102 (r'^polls/latest\.php$', 'myproject. apps.polls.views.index'),102 (r'^polls/latest\.php$', 'myproject.polls.views.index'), 103 103 104 104 But, don't do that. It's silly. … … 129 129 Fire up the Django development Web server:: 130 130 131 django-admin.py runserver --settings=myproject.settings131 python manage.py runserver 132 132 133 133 Now go to "http://localhost:8000/polls/" on your domain in your Web browser. … … 136 136 ViewDoesNotExist at /polls/ 137 137 138 Tried index in module myproject. apps.polls.views. Error was: 'module'138 Tried index in module myproject.polls.views. Error was: 'module' 139 139 object has no attribute 'index' 140 140 141 141 This error happened because you haven't written a function ``index()`` in the 142 module ``myproject/ apps/polls/views.py``.142 module ``myproject/polls/views.py``. 143 143 144 144 Try "/polls/23/", "/polls/23/results/" and "/polls/23/vote/". The error … … 146 146 haven't written any views yet). 147 147 148 Time to write the first view. Open the file ``myproject/ apps/polls/views.py``148 Time to write the first view. Open the file ``myproject/polls/views.py`` 149 149 and put the following Python code in it:: 150 150 … … 223 223 probably shouldn't make them public, just for security's sake. 224 224 225 Then edit ``TEMPLATE_DIRS`` in your settings file (``settings.py``) to tell226 Django where it can find templates -- just as you did in the "Customize the 227 admin look and feel"section of Tutorial 2.225 Then edit ``TEMPLATE_DIRS`` in your ``settings.py`` to tell Django where it can 226 find templates -- just as you did in the "Customize the admin look and feel" 227 section of Tutorial 2. 228 228 229 229 When you've done that, create a directory ``polls`` in your template directory. … … 386 386 387 387 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'), 392 392 ) 393 393 394 Namely, ``myproject. apps.polls.views`` is in every callback.394 Namely, ``myproject.polls.views`` is in every callback. 395 395 396 396 Because this is a common case, the URLconf framework provides a shortcut for … … 398 398 first argument to ``patterns()``, like so:: 399 399 400 urlpatterns = patterns('myproject. apps.polls.views',400 urlpatterns = patterns('myproject.polls.views', 401 401 (r'^polls/$', 'index'), 402 402 (r'^polls/(?P<poll_id>\d+)/$', 'detail'), … … 417 417 418 418 Our 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 is419 structure that ``python manage.py startapp`` created, but one part of it is 420 420 coupled to the Django settings: The URLconf. 421 421 … … 424 424 URLs within the app directory. 425 425 426 Copy the file ``myproject/urls.py`` to ``myproject/ apps/polls/urls.py``. Then,426 Copy the file ``myproject/urls.py`` to ``myproject/polls/urls.py``. Then, 427 427 change ``myproject/urls.py`` to remove the poll-specific URLs and insert an 428 428 ``include()``:: 429 429 430 (r'^polls/', include('myproject. apps.polls.urls')),430 (r'^polls/', include('myproject.polls.urls')), 431 431 432 432 ``include()``, simply, references another URLconf. Note that the regular … … 440 440 * Django will find the match at ``'^polls/'`` 441 441 * It will strip off the matching text (``"polls/"``) and send the remaining 442 text -- ``"34/"`` -- to the 'myproject. apps.polls.urls' urlconf for442 text -- ``"34/"`` -- to the 'myproject.polls.urls' urlconf for 443 443 further processing. 444 444 445 445 Now that we've decoupled that, we need to decouple the 446 'myproject. apps.polls.urls' urlconf by removing the leading "polls/" from each446 'myproject.polls.urls' urlconf by removing the leading "polls/" from each 447 447 line:: 448 448 449 urlpatterns = patterns('myproject. apps.polls.views',449 urlpatterns = patterns('myproject.polls.views', 450 450 (r'^$', 'index'), 451 451 (r'^(?P<poll_id>\d+)/$', 'detail'), django/trunk/docs/tutorial04.txt
r1258 r1901 45 45 included this line:: 46 46 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 49 So let's create a ``vote()`` function in ``myproject/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',161 urlpatterns = patterns('myproject.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.vote'),181 (r'^(?P<poll_id>\d+)/vote/$', 'myproject.polls.views.vote'), 182 182 ) 183 183
