Ticket #1464: magic-tutorial01-and-rst-title-fix.patch
File magic-tutorial01-and-rst-title-fix.patch, 12.9 KB (added by , 19 years ago) |
---|
-
tutorial01.txt
127 127 Run the following command to initialize your database with Django's core 128 128 database tables:: 129 129 130 python manage.py init131 130 python manage.py syncdb 131 132 132 If you don't see any errors, it worked. 133 133 134 134 If you're interested, run the command-line client for your database and type … … 137 137 138 138 .. admonition:: About those database tables 139 139 140 The tables created by ``manage.py init`` are for sessions, authentication 141 and other features Django provides. The next release of Django will have 142 a "lite" version of the ``init`` command that won't install any database 143 tables if you don't want them. 140 At this point, the tables created by ``manage.py syncdb`` are for sessions, authentication 141 and other features Django provides. 144 142 145 143 Creating models 146 144 =============== … … 176 174 177 175 polls/ 178 176 __init__.py 179 models/ 180 __init__.py 181 polls.py 177 models.py 182 178 views.py 183 179 184 180 This directory structure will house the poll application. … … 198 194 choice and a vote tally. Each choice is associated with a poll. 199 195 200 196 These concepts are represented by simple Python classes. Edit the 201 ``polls/models /polls.py`` file so it looks like this::197 ``polls/models.py`` file so it looks like this:: 202 198 203 from django. core import meta199 from django.db import models 204 200 205 class Poll(m eta.Model):206 question = m eta.CharField(maxlength=200)207 pub_date = m eta.DateTimeField('date published')201 class Poll(models.Model): 202 question = models.CharField(maxlength=200) 203 pub_date = models.DateTimeField('date published') 208 204 209 class Choice(m eta.Model):210 poll = m eta.ForeignKey(Poll)211 choice = m eta.CharField(maxlength=200)212 votes = m eta.IntegerField()205 class Choice(models.Model): 206 poll = models.ForeignKey(Poll) 207 choice = models.CharField(maxlength=200) 208 votes = models.IntegerField() 213 209 214 210 The code is straightforward. Each model is represented by a class that 215 subclasses ``django. core.meta.Model``. Each model has a number of class211 subclasses ``django.db.models.Model``. Each model has a number of class 216 212 variables, each of which represents a database field in the model. 217 213 218 Each field is represented by an instance of a ``m eta.*Field`` class -- e.g.,219 ``m eta.CharField`` for character fields and ``meta.DateTimeField`` for214 Each field is represented by an instance of a ``models.*Field`` class -- e.g., 215 ``models.CharField`` for character fields and ``models.DateTimeField`` for 220 216 datetimes. This tells Django what type of data each field holds. 221 217 222 The name of each ``m eta.*Field`` instance (e.g. ``question`` or ``pub_date`` )218 The name of each ``models.*Field`` instance (e.g. ``question`` or ``pub_date`` ) 223 219 is the field's name, in machine-friendly format. You'll use this value in your 224 220 Python code, and your database will use it as the column name. 225 221 … … 230 226 name for ``Poll.pub_date``. For all other fields in this model, the field's 231 227 machine-readable name will suffice as its human-readable name. 232 228 233 Some ``m eta.*Field`` classes have required elements. ``meta.CharField``, for229 Some ``models.*Field`` classes have required elements. ``models.CharField``, for 234 230 example, requires that you give it a ``maxlength``. That's used not only in the 235 231 database schema, but in validation, as we'll soon see. 236 232 237 Finally, note a relationship is defined, using ``m eta.ForeignKey``. That tells233 Finally, note a relationship is defined, using ``models.ForeignKey``. That tells 238 234 Django each Choice is related to a single Poll. Django supports all the common 239 235 database relationships: many-to-ones, many-to-manys and one-to-ones. 240 236 … … 275 271 You should see the following (the CREATE TABLE SQL statements for the polls app):: 276 272 277 273 BEGIN; 278 CREATE TABLE "polls_poll s" (274 CREATE TABLE "polls_poll" ( 279 275 "id" serial NOT NULL PRIMARY KEY, 280 276 "question" varchar(200) NOT NULL, 281 277 "pub_date" timestamp with time zone NOT NULL 282 278 ); 283 CREATE TABLE "polls_choice s" (279 CREATE TABLE "polls_choice" ( 284 280 "id" serial NOT NULL PRIMARY KEY, 285 281 "poll_id" integer NOT NULL REFERENCES "polls_polls" ("id"), 286 282 "choice" varchar(200) NOT NULL, … … 291 287 Note the following: 292 288 293 289 * Table names are automatically generated by combining the name of the app 294 (``polls``) with a plural version of the object name (polls and choices).290 (``polls``) and the name of the models (poll and choice). 295 291 (You can override this behavior.) 296 292 297 293 * Primary keys (IDs) are added automatically. (You can override this, too.) 298 294 299 * Django appends ``"_id"`` to the foreign key field name, by convention.295 * By convention, Django appends ``"_id"`` to the foreign key field name. 300 296 Yes, you can override this, as well. 301 297 302 298 * The foreign key relationship is made explicit by a ``REFERENCES`` statement. … … 306 302 ``integer primary key`` (SQLite) are handled for you automatically. Same 307 303 goes for quoting of field names -- e.g., using double quotes or single 308 304 quotes. The author of this tutorial runs PostgreSQL, so the example 309 output is in PostgreSQL syntax.305 output is in PostgreSQL syntax. 310 306 311 307 If you're interested, also run the following commands: 312 308 313 * ``python manage.py sqlinitialdata polls`` -- Outputs the initial-data314 inserts required for Django's admin framework.309 * ``python manage.py sqlinitialdata polls`` -- Outputs any initial data 310 required for Django's admin framework and your models. 315 311 316 312 * ``python manage.py sqlclear polls`` -- Outputs the necessary ``DROP 317 313 TABLE`` statements for this app, according to which tables already exist … … 326 322 Looking at the output of those commands can help you understand what's actually 327 323 happening under the hood. 328 324 329 Now, run this command to create the database tables for the polls app 330 automatically:: 325 Now, run ``syncdb`` again to create the model tables in your database:: 331 326 332 python manage.py install polls327 python manage.py syncdb 333 328 334 Behind the scenes, all that command does is take the output of 335 ``python manage.py sqlall polls`` and execute it in the database pointed-to by 336 your Django settings file. 329 In addition to initializing Django's core tables, ``syncdb`` inspects the apps listed in INSTALLED_APPS, and, for each new app, executes the output of the respective ``python manage.py sqlall`` command against your database. 337 330 338 331 Read the `django-admin.py documentation`_ for full information on what the 339 332 ``manage.py`` utility can do. … … 376 369 377 370 # Modules are dynamically created within django.models. 378 371 # Their names are plural versions of the model class names. 379 >>> from django.models.polls import polls, choices372 >>> from myproject.polls import Poll, Choice 380 373 381 374 # No polls are in the system yet. 382 >>> polls. get_list()375 >>> polls.objects.all() 383 376 [] 384 377 385 378 # Create a new Poll. 386 379 >>> from datetime import datetime 387 >>> p = polls.Poll(question="What's up?", pub_date=datetime.now())380 >>> p = Poll(question="What's up?", pub_date=datetime.now()) 388 381 389 382 # Save the object into the database. You have to call save() explicitly. 390 383 >>> p.save() … … 407 400 >>> p.save() 408 401 409 402 # get_list() displays all the polls in the database. 410 >>> polls.get_list()403 >>> Poll.objects.all() 411 404 [<Poll object>] 412 405 406 413 407 Wait a minute. ``<Poll object>`` is, utterly, an unhelpful representation of 414 408 this object. Let's fix that by editing the polls model 415 (in the ``polls/models /polls.py`` file) and adding a ``__repr__()`` method to409 (in the ``polls/models.py`` file) and adding a ``__repr__()`` method to 416 410 both ``Poll`` and ``Choice``:: 417 411 418 class Poll(m eta.Model):412 class Poll(models.Model): 419 413 # ... 420 414 def __repr__(self): 421 415 return self.question 422 416 423 class Choice(m eta.Model):417 class Choice(models.Model): 424 418 # ... 425 419 def __repr__(self): 426 420 return self.choice … … 432 426 Note these are normal Python methods. Let's add a custom method, just for 433 427 demonstration:: 434 428 435 class Poll(m eta.Model):429 class Poll(models.Model): 436 430 # ... 437 431 def was_published_today(self): 438 432 return self.pub_date.date() == datetime.date.today() 439 433 440 Note ``import datetime`` wasn't necessary. Each model method has access to 441 a handful of commonly-used variables for convenience, including the 442 ``datetime`` module from the Python standard library. 434 Note the addition of ``import datetime`` to reference Python's standard datetime module. 443 435 444 436 Let's jump back into the Python interactive shell by running 445 437 ``python manage.py shell`` again:: 446 438 447 >>> from django.models.polls import polls, choices439 >>> from polls.models import Poll, Choice 448 440 # Make sure our __repr__() addition worked. 449 >>> polls. get_list()441 >>> polls.objects.all() 450 442 [What's up?] 451 443 452 444 # Django provides a rich database lookup API that's entirely driven by 453 445 # keyword arguments. 454 >>> polls.get_object(id__exact=1)455 What's up?456 >>> polls.get_object(question__startswith='What')457 What's up?446 >>> Poll.objects.filter(id__exact=1) 447 [What's up?] 448 >>> Poll.objects.filter(question__startswith='What') 449 [What's up?] 458 450 459 451 # Get the poll whose year is 2005. Of course, if you're going through this 460 452 # tutorial in another year, change as appropriate. 461 >>> polls.get_object(pub_date__year=2005)453 >>> Poll.objects.get(pub_date__year=2005) 462 454 What's up? 463 455 464 >>> polls.get_object(id__exact=2)456 >>> Poll.objects.get(id__exact=2) 465 457 Traceback (most recent call last): 466 458 ... 467 PollDoesNotExist: Poll does not exist for {'id__exact': 2}468 >>> polls.get_list(question__startswith='What')459 DoesNotExist: Poll does not exist for {'id__exact': 2} 460 >>> Poll.objects.filter(question__startswith='What') 469 461 [What's up?] 470 462 471 463 # Lookup by a primary key is the most common case, so Django provides a 472 464 # shortcut for primary-key exact lookups. 473 # The following is identical to polls.get_object(id__exact=1).474 >>> polls.get_object(pk=1)465 # The following is identical to Poll.objects.get(id__exact=1). 466 >>> Poll.objects.get(pk=1) 475 467 What's up? 476 468 477 469 # Make sure our custom method worked. 478 >>> p = polls.get_object(pk=1)470 >>> p = Poll.objects.get(pk=1) 479 471 >>> p.was_published_today() 480 472 False 481 473 482 474 # Give the Poll a couple of Choices. Each one of these method calls does an 483 475 # INSERT statement behind the scenes and returns the new Choice object. 484 >>> p = polls.get_object(pk=1)485 >>> p. add_choice(choice='Not much', votes=0)476 >>> p = Poll.objects.get(pk=1) 477 >>> p.choice_set.add(choice='Not much', votes=0) 486 478 Not much 487 >>> p. add_choice(choice='The sky', votes=0)479 >>> p.choice_set.add(choice='The sky', votes=0) 488 480 The sky 489 >>> c = p. add_choice(choice='Just hacking again', votes=0)481 >>> c = p.choice_set.add(choice='Just hacking again', votes=0) 490 482 491 483 # Choice objects have API access to their related Poll objects. 492 >>> c. get_poll()484 >>> c.poll 493 485 What's up? 494 486 495 487 # And vice versa: Poll objects get access to Choice objects. 496 >>> p. get_choice_list()488 >>> p.choice_set.all() 497 489 [Not much, The sky, Just hacking again] 498 >>> p. get_choice_count()490 >>> p.choice_set.all().count() 499 491 3 500 492 501 493 # The API automatically follows relationships as far as you need. 502 494 # Use double underscores to separate relationships. 503 495 # This works as many levels deep as you want. There's no limit. 504 496 # Find all Choices for any poll whose pub_date is in 2005. 505 >>> choices.get_list(poll__pub_date__year=2005)497 >>> Choice.objects.filter(poll__pub_date__year=2005) 506 498 [Not much, The sky, Just hacking again] 507 499 508 500 # Let's delete one of the choices. Use delete() for that. 509 >>> c = p. get_choice(choice__startswith='Just hacking')501 >>> c = p.choice_set.filter(choice__startswith='Just hacking') 510 502 >>> c.delete() 511 503 512 504 For full details on the database API, see our `Database API reference`_. -
middleware.txt
96 96 ``Date`` and ``Content-Length`` response-headers. 97 97 98 98 django.contrib.sessions.middleware.SessionMiddleware 99 -------------------------------------------- 99 ---------------------------------------------------- 100 100 101 101 Enables session support. See the `session documentation`_. 102 102 -
templates_python.txt
256 256 you'll see below. 257 257 258 258 Subclassing Context: RequestContext 259 ---------------------------------- 259 ----------------------------------- 260 260 261 261 Django comes with a special ``Context`` class, 262 262 ``django.template.RequestContext``, that acts slightly differently than