Ticket #17715: 17715.diff

File 17715.diff, 12.4 KB (added by Aymeric Augustin, 12 years ago)
  • docs/intro/tutorial01.txt

     
    219219  an empty string if your database server is on the same physical
    220220  machine (not used for SQLite).
    221221
    222 If you're new to databases, we recommend simply using SQLite (by
    223 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 to
    225 install anything else.
     222If 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
     225you won't need to install anything else.
    226226
    227227.. note::
    228228
     
    233233    If you're using SQLite, you don't need to create anything beforehand - the
    234234    database file will be created automatically when it is needed.
    235235
    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.
     236While you're editing :file:`settings.py`, set :setting:`TIME_ZONE` to your
     237time zone. The default value isn't correct for you, unless you happen to live
     238near Chicago.
    241239
     240Also, take note of the :setting:`INSTALLED_APPS` setting towards the bottom of
     241the file. That variable holds the names of all Django applications that are
     242activated in this Django instance. Apps can be used in multiple projects, and
     243you can package and distribute them for use by others in their projects.
     244
    242245By default, :setting:`INSTALLED_APPS` contains the following apps, all of which
    243246come with Django:
    244247
     
    414417        'django.contrib.contenttypes',
    415418        'django.contrib.sessions',
    416419        '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',
    417426        'polls',
    418427    )
    419428
     
    437446    );
    438447    CREATE TABLE "polls_choice" (
    439448        "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,
    441450        "choice" varchar(200) NOT NULL,
    442451        "votes" integer NOT NULL
    443452    );
     
    454463* Primary keys (IDs) are added automatically. (You can override this, too.)
    455464
    456465* 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.)
    458467
    459468* The foreign key relationship is made explicit by a ``REFERENCES``
    460469  statement.
     
    501510
    502511    python manage.py syncdb
    503512
    504 The :djadmin:`syncdb` command runs the sql from 'sqlall' on your database for
    505 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 added to your project since the last time you ran syncdb. :djadmin:`syncdb` can
    508 be called as often as you like, and it will only ever create the tables that
    509 don't exist.
     513The :djadmin:`syncdb` command runs the sql from :djadmin:`sqlall` on your
     514database for all apps in :setting:`INSTALLED_APPS` that don't already exist in
     515your database. This creates all the tables, initial data and indexes for any
     516apps 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
     518create the tables that don't exist.
    510519
    511520Read the :doc:`django-admin.py documentation </ref/django-admin>` for full
    512521information on what the ``manage.py`` utility can do.
     
    537546
    538547Once you're in the shell, explore the :doc:`database API </topics/db/queries>`::
    539548
    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.
    541550
    542551    # No polls are in the system yet.
    543552    >>> Poll.objects.all()
    544553    []
    545554
    546555    # 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())
    549562
    550563    # Save the object into the database. You have to call save() explicitly.
    551564    >>> p.save()
     
    559572
    560573    # Access database columns via Python attributes.
    561574    >>> p.question
    562     "What's up?"
     575    "What's new?"
    563576    >>> p.pub_date
    564     datetime.datetime(2007, 7, 15, 12, 00, 53)
     577    datetime.datetime(2012, 7, 15, 12, 0, 53, 775217, tzinfo=<UTC>)
    565578
    566579    # 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?"
    568581    >>> p.save()
    569582
    570583    # objects.all() displays all the polls in the database.
     
    617630demonstration::
    618631
    619632    import datetime
     633    from django.utils import timezone
    620634    # ...
    621635    class Poll(models.Model):
    622636        # ...
    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)
    625639
    626 Note the addition of ``import datetime`` to reference Python's standard
    627 ``datetime`` module.
     640Note the addition of ``import datetime`` and ``from django.utils import
     641timezone``, to reference Python's standard ``datetime`` module and Django's
     642time zone-related utilities respectively.
    628643
    629644Save these changes and start a new Python interactive shell by running
    630645``python manage.py shell`` again::
     
    642657    >>> Poll.objects.filter(question__startswith='What')
    643658    [<Poll: What's up?>]
    644659
    645     # Get the poll whose year is 2007.
    646     >>> Poll.objects.get(pub_date__year=2007)
     660    # Get the poll whose year is 2012.
     661    >>> Poll.objects.get(pub_date__year=2012)
    647662    <Poll: What's up?>
    648663
    649664    >>> Poll.objects.get(id=2)
     
    659674
    660675    # Make sure our custom method worked.
    661676    >>> p = Poll.objects.get(pk=1)
    662     >>> p.was_published_today()
    663     False
     677    >>> p.was_published_recently()
     678    True
    664679
    665680    # Give the Poll a couple of Choices. The create call constructs a new
    666681    # choice object, does the INSERT statement, adds the choice to the set
     
    693708    # The API automatically follows relationships as far as you need.
    694709    # Use double underscores to separate relationships.
    695710    # 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 2007.
    697     >>> Choice.objects.filter(poll__pub_date__year=2007)
     711    # Find all Choices for any poll whose pub_date is in 2012.
     712    >>> Choice.objects.filter(poll__pub_date__year=2012)
    698713    [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
    699714
    700715    # Let's delete one of the choices. Use delete() for that.
  • docs/intro/tutorial02.txt

     
    1818    displayed on the public site. Django solves the problem of creating a
    1919    unified interface for site administrators to edit content.
    2020
    21     The admin isn't necessarily intended to be used by site visitors; it's for
    22     site managers.
     21    The admin isn't intended to be used by site visitors; it's for site
     22    managers.
    2323
    2424Activate the admin site
    2525=======================
     
    2727The Django admin site is not activated by default -- it's an opt-in thing. To
    2828activate the admin site for your installation, do these three things:
    2929
    30 * Add ``"django.contrib.admin"`` to your :setting:`INSTALLED_APPS` setting.
     30* Uncomment ``"django.contrib.admin"`` in the :setting:`INSTALLED_APPS` setting.
    3131
    3232* Run ``python manage.py syncdb``. Since you have added a new application
    3333  to :setting:`INSTALLED_APPS`, the database tables need to be updated.
     
    101101.. image:: _images/admin02t.png
    102102   :alt: Django admin index page
    103103
    104 You should see a few other types of editable content, including groups, users
     104You should see a few types of editable content, including groups, users
    105105and sites. These are core features Django ships with by default.
    106106
    107107Make the poll app modifiable in the admin
     
    169169
    170170* Delete -- Displays a delete confirmation page.
    171171
     172If the value of "Date published" doesn't match the time when you created the
     173poll in Tutorial 1, it probably means you forgot to set the correct value for
     174the :setting:`TIME_ZONE` setting. Change it, reload the page, and check that
     175the correct value appears.
     176
    172177Change the "Date published" by clicking the "Today" and "Now" shortcuts. Then
    173178click "Save and continue editing." Then click "History" in the upper right.
    174179You'll see a page listing all changes made to this object via the Django admin,
     
    337342        # ...
    338343        list_display = ('question', 'pub_date')
    339344
    340 Just for good measure, let's also include the ``was_published_today`` custom
     345Just for good measure, let's also include the ``was_published_recently`` custom
    341346method from Tutorial 1::
    342347
    343348    class PollAdmin(admin.ModelAdmin):
    344349        # ...
    345         list_display = ('question', 'pub_date', 'was_published_today')
     350        list_display = ('question', 'pub_date', 'was_published_recently')
    346351
    347352Now the poll change list page looks like this:
    348353
     
    350355   :alt: Polls change list page, updated
    351356
    352357You 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 of
     358case of the ``was_published_recently`` header, because sorting by the output of
    354359an arbitrary method is not supported. Also note that the column header for
    355 ``was_published_today`` is, by default, the name of the method (with
     360``was_published_recently`` is, by default, the name of the method (with
    356361underscores replaced with spaces). But you can change that by giving that
    357362method (in ``models.py``) a ``short_description`` attribute::
    358363
    359364    class Poll(models.Model):
    360365        # ...
    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?'
    364369
    365370Edit your admin.py file again and add an improvement to the Poll change list page: Filters. Add the
    366371following line to ``PollAdmin``::
     
    374379   :alt: Polls change list page, updated
    375380
    376381The 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 default
    378 filter options for DateTimeFields: "Any date," "Today," "Past 7 days,"
    379 "This month," "This year."
     382Because ``pub_date`` is a :class:`~django.db.models.fields.DateTimeField`,
     383Django knows to give the default filter options for ``DateTimeField``s: "Any
     384date," "Today," "Past 7 days," "This month," "This year."
    380385
    381386This is shaping up well. Let's add some search capability::
    382387
  • django/utils/timezone.py

     
    3232    Used only when pytz isn't available.
    3333    """
    3434
     35    def __repr__(self):
     36        return "<UTC>"
     37
    3538    def utcoffset(self, dt):
    3639        return ZERO
    3740
     
    6063        self.DSTDIFF = self.DSTOFFSET - self.STDOFFSET
    6164        tzinfo.__init__(self)
    6265
     66    def __repr__(self):
     67        return "<LocalTimezone>"
     68
    6369    def utcoffset(self, dt):
    6470        if self._isdst(dt):
    6571            return self.DSTOFFSET
Back to Top