Ticket #17715: 17715.diff
File 17715.diff, 12.4 KB (added by , 13 years ago) |
---|
-
docs/intro/tutorial01.txt
219 219 an empty string if your database server is on the same physical 220 220 machine (not used for SQLite). 221 221 222 If you're new to databases, we recommend simply using SQLite (by223 setting :setting:`ENGINE` to ``'django.db.backends.sqlite3'``). SQLite 224 is included as part of Python 2.5 and later, so you won't need to225 install anything else.222 If you're new to databases, we recommend simply using SQLite by setting 223 :setting:`ENGINE` to ``'django.db.backends.sqlite3'`` and :setting:`NAME` to 224 ``'database.sqlite3'``. SQLite is included as part of Python 2.5 and later, so 225 you won't need to install anything else. 226 226 227 227 .. note:: 228 228 … … 233 233 If you're using SQLite, you don't need to create anything beforehand - the 234 234 database file will be created automatically when it is needed. 235 235 236 While you're editing :file:`settings.py`, take note of the 237 :setting:`INSTALLED_APPS` setting towards the bottom of the file. That variable 238 holds the names of all Django applications that are activated in this Django 239 instance. Apps can be used in multiple projects, and you can package and 240 distribute them for use by others in their projects. 236 While you're editing :file:`settings.py`, set :setting:`TIME_ZONE` to your 237 time zone. The default value isn't correct for you, unless you happen to live 238 near Chicago. 241 239 240 Also, take note of the :setting:`INSTALLED_APPS` setting towards the bottom of 241 the file. That variable holds the names of all Django applications that are 242 activated in this Django instance. Apps can be used in multiple projects, and 243 you can package and distribute them for use by others in their projects. 244 242 245 By default, :setting:`INSTALLED_APPS` contains the following apps, all of which 243 246 come with Django: 244 247 … … 414 417 'django.contrib.contenttypes', 415 418 'django.contrib.sessions', 416 419 'django.contrib.sites', 420 'django.contrib.messages', 421 'django.contrib.staticfiles', 422 # Uncomment the next line to enable the admin: 423 # 'django.contrib.admin', 424 # Uncomment the next line to enable admin documentation: 425 # 'django.contrib.admindocs', 417 426 'polls', 418 427 ) 419 428 … … 437 446 ); 438 447 CREATE TABLE "polls_choice" ( 439 448 "id" serial NOT NULL PRIMARY KEY, 440 "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id") ,449 "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id") DEFERRABLE INITIALLY DEFERRED, 441 450 "choice" varchar(200) NOT NULL, 442 451 "votes" integer NOT NULL 443 452 ); … … 454 463 * Primary keys (IDs) are added automatically. (You can override this, too.) 455 464 456 465 * By convention, Django appends ``"_id"`` to the foreign key field name. 457 Yes, you can override this, as well.466 (Yes, you can override this, as well.) 458 467 459 468 * The foreign key relationship is made explicit by a ``REFERENCES`` 460 469 statement. … … 501 510 502 511 python manage.py syncdb 503 512 504 The :djadmin:`syncdb` command runs the sql from 'sqlall' on your database for505 all apps in :setting:`INSTALLED_APPS` that don't already exist in your database. 506 This creates all the tables, initial data and indexes for any apps you have 507 a dded to your project since the last time you ran syncdb. :djadmin:`syncdb` can508 be called as often as you like, and it will only ever create the tables that 509 don't exist.513 The :djadmin:`syncdb` command runs the sql from :djadmin:`sqlall` on your 514 database for all apps in :setting:`INSTALLED_APPS` that don't already exist in 515 your database. This creates all the tables, initial data and indexes for any 516 apps you have added to your project since the last time you ran syncdb. 517 :djadmin:`syncdb` can be called as often as you like, and it will only ever 518 create the tables that don't exist. 510 519 511 520 Read the :doc:`django-admin.py documentation </ref/django-admin>` for full 512 521 information on what the ``manage.py`` utility can do. … … 537 546 538 547 Once you're in the shell, explore the :doc:`database API </topics/db/queries>`:: 539 548 540 >>> from polls.models import Poll, Choice # Import the model classes we just wrote.549 >>> from polls.models import Poll, Choice # Import the model classes we just wrote. 541 550 542 551 # No polls are in the system yet. 543 552 >>> Poll.objects.all() 544 553 [] 545 554 546 555 # Create a new Poll. 547 >>> import datetime 548 >>> p = Poll(question="What's up?", pub_date=datetime.datetime.now()) 556 # Support for time zones is enabled in the default settings file, so 557 # Django expects a datetime with a tzinfo attribute in pub_date. Just use 558 # timezone.now() instead of datetime.datetime.now() for now; it will do 559 # the right thing in all circumstances. See the time zone docs for more. 560 >>> from django.utils import timezone 561 >>> p = Poll(question="What's new?", pub_date=timezone.now()) 549 562 550 563 # Save the object into the database. You have to call save() explicitly. 551 564 >>> p.save() … … 559 572 560 573 # Access database columns via Python attributes. 561 574 >>> p.question 562 "What's up?"575 "What's new?" 563 576 >>> p.pub_date 564 datetime.datetime(20 07, 7, 15, 12, 00, 53)577 datetime.datetime(2012, 7, 15, 12, 0, 53, 775217, tzinfo=<UTC>) 565 578 566 579 # Change values by changing the attributes, then calling save(). 567 >>> p. pub_date = datetime.datetime(2007, 4, 1, 0, 0)580 >>> p.question = "What's up?" 568 581 >>> p.save() 569 582 570 583 # objects.all() displays all the polls in the database. … … 617 630 demonstration:: 618 631 619 632 import datetime 633 from django.utils import timezone 620 634 # ... 621 635 class Poll(models.Model): 622 636 # ... 623 def was_published_ today(self):624 return self.pub_date .date() == datetime.date.today()637 def was_published_recently(self): 638 return self.pub_date >= timezone.now() - datetime.timedelta(days=1) 625 639 626 Note the addition of ``import datetime`` to reference Python's standard 627 ``datetime`` module. 640 Note the addition of ``import datetime`` and ``from django.utils import 641 timezone``, to reference Python's standard ``datetime`` module and Django's 642 time zone-related utilities respectively. 628 643 629 644 Save these changes and start a new Python interactive shell by running 630 645 ``python manage.py shell`` again:: … … 642 657 >>> Poll.objects.filter(question__startswith='What') 643 658 [<Poll: What's up?>] 644 659 645 # Get the poll whose year is 20 07.646 >>> Poll.objects.get(pub_date__year=20 07)660 # Get the poll whose year is 2012. 661 >>> Poll.objects.get(pub_date__year=2012) 647 662 <Poll: What's up?> 648 663 649 664 >>> Poll.objects.get(id=2) … … 659 674 660 675 # Make sure our custom method worked. 661 676 >>> p = Poll.objects.get(pk=1) 662 >>> p.was_published_ today()663 False677 >>> p.was_published_recently() 678 True 664 679 665 680 # Give the Poll a couple of Choices. The create call constructs a new 666 681 # choice object, does the INSERT statement, adds the choice to the set … … 693 708 # The API automatically follows relationships as far as you need. 694 709 # Use double underscores to separate relationships. 695 710 # This works as many levels deep as you want; there's no limit. 696 # Find all Choices for any poll whose pub_date is in 20 07.697 >>> Choice.objects.filter(poll__pub_date__year=20 07)711 # Find all Choices for any poll whose pub_date is in 2012. 712 >>> Choice.objects.filter(poll__pub_date__year=2012) 698 713 [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>] 699 714 700 715 # Let's delete one of the choices. Use delete() for that. -
docs/intro/tutorial02.txt
18 18 displayed on the public site. Django solves the problem of creating a 19 19 unified interface for site administrators to edit content. 20 20 21 The admin isn't necessarily intended to be used by site visitors; it's for22 sitemanagers.21 The admin isn't intended to be used by site visitors; it's for site 22 managers. 23 23 24 24 Activate the admin site 25 25 ======================= … … 27 27 The Django admin site is not activated by default -- it's an opt-in thing. To 28 28 activate the admin site for your installation, do these three things: 29 29 30 * Add ``"django.contrib.admin"`` to your:setting:`INSTALLED_APPS` setting.30 * Uncomment ``"django.contrib.admin"`` in the :setting:`INSTALLED_APPS` setting. 31 31 32 32 * Run ``python manage.py syncdb``. Since you have added a new application 33 33 to :setting:`INSTALLED_APPS`, the database tables need to be updated. … … 101 101 .. image:: _images/admin02t.png 102 102 :alt: Django admin index page 103 103 104 You should see a few othertypes of editable content, including groups, users104 You should see a few types of editable content, including groups, users 105 105 and sites. These are core features Django ships with by default. 106 106 107 107 Make the poll app modifiable in the admin … … 169 169 170 170 * Delete -- Displays a delete confirmation page. 171 171 172 If the value of "Date published" doesn't match the time when you created the 173 poll in Tutorial 1, it probably means you forgot to set the correct value for 174 the :setting:`TIME_ZONE` setting. Change it, reload the page, and check that 175 the correct value appears. 176 172 177 Change the "Date published" by clicking the "Today" and "Now" shortcuts. Then 173 178 click "Save and continue editing." Then click "History" in the upper right. 174 179 You'll see a page listing all changes made to this object via the Django admin, … … 337 342 # ... 338 343 list_display = ('question', 'pub_date') 339 344 340 Just for good measure, let's also include the ``was_published_ today`` custom345 Just for good measure, let's also include the ``was_published_recently`` custom 341 346 method from Tutorial 1:: 342 347 343 348 class PollAdmin(admin.ModelAdmin): 344 349 # ... 345 list_display = ('question', 'pub_date', 'was_published_ today')350 list_display = ('question', 'pub_date', 'was_published_recently') 346 351 347 352 Now the poll change list page looks like this: 348 353 … … 350 355 :alt: Polls change list page, updated 351 356 352 357 You can click on the column headers to sort by those values -- except in the 353 case of the ``was_published_ today`` header, because sorting by the output of358 case of the ``was_published_recently`` header, because sorting by the output of 354 359 an arbitrary method is not supported. Also note that the column header for 355 ``was_published_ today`` is, by default, the name of the method (with360 ``was_published_recently`` is, by default, the name of the method (with 356 361 underscores replaced with spaces). But you can change that by giving that 357 362 method (in ``models.py``) a ``short_description`` attribute:: 358 363 359 364 class Poll(models.Model): 360 365 # ... 361 def was_published_ today(self):362 return self.pub_date .date() == datetime.date.today()363 was_published_ today.short_description = 'Published today?'366 def was_published_recently(self): 367 return self.pub_date >= timezone.now() - datetime.timedelta(days=1) 368 was_published_recently.short_description = 'Published less than 1 day ago?' 364 369 365 370 Edit your admin.py file again and add an improvement to the Poll change list page: Filters. Add the 366 371 following line to ``PollAdmin``:: … … 374 379 :alt: Polls change list page, updated 375 380 376 381 The type of filter displayed depends on the type of field you're filtering on. 377 Because ``pub_date`` is a DateTimeField, Django knows to give the default378 filter options for DateTimeFields: "Any date," "Today," "Past 7 days," 379 "This month," "This year."382 Because ``pub_date`` is a :class:`~django.db.models.fields.DateTimeField`, 383 Django knows to give the default filter options for ``DateTimeField``s: "Any 384 date," "Today," "Past 7 days," "This month," "This year." 380 385 381 386 This is shaping up well. Let's add some search capability:: 382 387 -
django/utils/timezone.py
32 32 Used only when pytz isn't available. 33 33 """ 34 34 35 def __repr__(self): 36 return "<UTC>" 37 35 38 def utcoffset(self, dt): 36 39 return ZERO 37 40 … … 60 63 self.DSTDIFF = self.DSTOFFSET - self.STDOFFSET 61 64 tzinfo.__init__(self) 62 65 66 def __repr__(self): 67 return "<LocalTimezone>" 68 63 69 def utcoffset(self, dt): 64 70 if self._isdst(dt): 65 71 return self.DSTOFFSET