|
@@ -70,7 +70,7 @@ This will create a ``mysite`` directory in your current directory.
|
|
|
|
|
|
:doc:`django-admin.py </ref/django-admin>` should be on your system path if you
|
|
|
installed Django via ``python setup.py``. If it's not on your path, you can find
|
|
|
-it in ``site-packages/django/bin``, where ```site-packages``` is a directory
|
|
|
+it in ``site-packages/django/bin``, where ``site-packages`` is a directory
|
|
|
within your Python installation. Consider symlinking to :doc:`django-admin.py
|
|
|
</ref/django-admin>` from some place on your path, such as
|
|
|
:file:`/usr/local/bin`.
|
|
@@ -192,13 +192,13 @@ Database setup
|
|
|
Now, edit :file:`mysite/settings.py`. It's a normal Python module with
|
|
|
module-level variables representing Django settings. Change the
|
|
|
following keys in the :setting:`DATABASES` ``'default'`` item to match
|
|
|
-your databases connection settings.
|
|
|
+your database connection settings.
|
|
|
|
|
|
* :setting:`ENGINE <DATABASE-ENGINE>` -- Either
|
|
|
``'django.db.backends.postgresql_psycopg2'``,
|
|
|
- ``'django.db.backends.mysql'`` or
|
|
|
- ``'django.db.backends.sqlite3'``. Other backends are
|
|
|
- :setting:`also available <DATABASE-ENGINE>`.
|
|
|
+ ``'django.db.backends.mysql'``, ``'django.db.backends.sqlite3'`` or
|
|
|
+ ``'django.db.backends.oracle'``. Other backends are :setting:`also available
|
|
|
+ <DATABASE-ENGINE>`.
|
|
|
|
|
|
* :setting:`NAME` -- The name of your database. If you're using
|
|
|
SQLite, the database will be a file on your computer; in that
|
|
@@ -219,10 +219,10 @@ your databases connection settings.
|
|
|
an empty string if your database server is on the same physical
|
|
|
machine (not used for SQLite).
|
|
|
|
|
|
-If you're new to databases, we recommend simply using SQLite (by
|
|
|
-setting :setting:`ENGINE` to ``'django.db.backends.sqlite3'``). SQLite
|
|
|
-is included as part of Python 2.5 and later, so you won't need to
|
|
|
-install anything else.
|
|
|
+If you're new to databases, we recommend simply using SQLite by setting
|
|
|
+:setting:`ENGINE` to ``'django.db.backends.sqlite3'`` and :setting:`NAME` to
|
|
|
+the place where you'd like to store the database. SQLite is included as part
|
|
|
+of Python 2.5 and later, so you won't need to install anything else.
|
|
|
|
|
|
.. note::
|
|
|
|
|
@@ -233,11 +233,14 @@ install anything else.
|
|
|
If you're using SQLite, you don't need to create anything beforehand - the
|
|
|
database file will be created automatically when it is needed.
|
|
|
|
|
|
-While you're editing :file:`settings.py`, take note of the
|
|
|
-:setting:`INSTALLED_APPS` setting towards the bottom of the file. That variable
|
|
|
-holds the names of all Django applications that are activated in this Django
|
|
|
-instance. Apps can be used in multiple projects, and you can package and
|
|
|
-distribute them for use by others in their projects.
|
|
|
+While you're editing :file:`settings.py`, set :setting:`TIME_ZONE` to your
|
|
|
+time zone. The default value isn't correct for you, unless you happen to live
|
|
|
+near Chicago.
|
|
|
+
|
|
|
+Also, take note of the :setting:`INSTALLED_APPS` setting towards the bottom of
|
|
|
+the file. That variable holds the names of all Django applications that are
|
|
|
+activated in this Django instance. Apps can be used in multiple projects, and
|
|
|
+you can package and distribute them for use by others in their projects.
|
|
|
|
|
|
By default, :setting:`INSTALLED_APPS` contains the following apps, all of which
|
|
|
come with Django:
|
|
@@ -414,6 +417,12 @@ it'll look like this::
|
|
|
'django.contrib.contenttypes',
|
|
|
'django.contrib.sessions',
|
|
|
'django.contrib.sites',
|
|
|
+ 'django.contrib.messages',
|
|
|
+ 'django.contrib.staticfiles',
|
|
|
+ # Uncomment the next line to enable the admin:
|
|
|
+ # 'django.contrib.admin',
|
|
|
+ # Uncomment the next line to enable admin documentation:
|
|
|
+ # 'django.contrib.admindocs',
|
|
|
'polls',
|
|
|
)
|
|
|
|
|
@@ -437,7 +446,7 @@ statements for the polls app):
|
|
|
);
|
|
|
CREATE TABLE "polls_choice" (
|
|
|
"id" serial NOT NULL PRIMARY KEY,
|
|
|
- "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id"),
|
|
|
+ "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id") DEFERRABLE INITIALLY DEFERRED,
|
|
|
"choice" varchar(200) NOT NULL,
|
|
|
"votes" integer NOT NULL
|
|
|
);
|
|
@@ -454,7 +463,7 @@ Note the following:
|
|
|
* Primary keys (IDs) are added automatically. (You can override this, too.)
|
|
|
|
|
|
* By convention, Django appends ``"_id"`` to the foreign key field name.
|
|
|
- Yes, you can override this, as well.
|
|
|
+ (Yes, you can override this, as well.)
|
|
|
|
|
|
* The foreign key relationship is made explicit by a ``REFERENCES``
|
|
|
statement.
|
|
@@ -501,12 +510,12 @@ Now, run :djadmin:`syncdb` again to create those model tables in your database:
|
|
|
|
|
|
python manage.py syncdb
|
|
|
|
|
|
-The :djadmin:`syncdb` command runs the sql from 'sqlall' on your database for
|
|
|
-all apps in :setting:`INSTALLED_APPS` that don't already exist in your database.
|
|
|
-This creates all the tables, initial data and indexes for any apps you have
|
|
|
-added to your project since the last time you ran syncdb. :djadmin:`syncdb` can
|
|
|
-be called as often as you like, and it will only ever create the tables that
|
|
|
-don't exist.
|
|
|
+The :djadmin:`syncdb` command runs the sql from :djadmin:`sqlall` on your
|
|
|
+database for all apps in :setting:`INSTALLED_APPS` that don't already exist in
|
|
|
+your database. This creates all the tables, initial data and indexes for any
|
|
|
+apps you have added to your project since the last time you ran syncdb.
|
|
|
+:djadmin:`syncdb` can be called as often as you like, and it will only ever
|
|
|
+create the tables that don't exist.
|
|
|
|
|
|
Read the :doc:`django-admin.py documentation </ref/django-admin>` for full
|
|
|
information on what the ``manage.py`` utility can do.
|
|
@@ -537,15 +546,18 @@ the Python import path to your :file:`settings.py` file.
|
|
|
|
|
|
Once you're in the shell, explore the :doc:`database API </topics/db/queries>`::
|
|
|
|
|
|
- >>> from polls.models import Poll, Choice # Import the model classes we just wrote.
|
|
|
+ >>> from polls.models import Poll, Choice # Import the model classes we just wrote.
|
|
|
|
|
|
# No polls are in the system yet.
|
|
|
>>> Poll.objects.all()
|
|
|
[]
|
|
|
|
|
|
# Create a new Poll.
|
|
|
- >>> import datetime
|
|
|
- >>> p = Poll(question="What's up?", pub_date=datetime.datetime.now())
|
|
|
+ # Support for time zones is enabled in the default settings file, so
|
|
|
+ # Django expects a datetime with tzinfo for pub_date. Use timezone.now()
|
|
|
+ # instead of datetime.datetime.now() and it will do the right thing.
|
|
|
+ >>> from django.utils import timezone
|
|
|
+ >>> p = Poll(question="What's new?", pub_date=timezone.now())
|
|
|
|
|
|
# Save the object into the database. You have to call save() explicitly.
|
|
|
>>> p.save()
|
|
@@ -559,12 +571,12 @@ Once you're in the shell, explore the :doc:`database API </topics/db/queries>`::
|
|
|
|
|
|
# Access database columns via Python attributes.
|
|
|
>>> p.question
|
|
|
- "What's up?"
|
|
|
+ "What's new?"
|
|
|
>>> p.pub_date
|
|
|
- datetime.datetime(2007, 7, 15, 12, 00, 53)
|
|
|
+ datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)
|
|
|
|
|
|
# Change values by changing the attributes, then calling save().
|
|
|
- >>> p.pub_date = datetime.datetime(2007, 4, 1, 0, 0)
|
|
|
+ >>> p.question = "What's up?"
|
|
|
>>> p.save()
|
|
|
|
|
|
# objects.all() displays all the polls in the database.
|
|
@@ -617,14 +629,18 @@ Note these are normal Python methods. Let's add a custom method, just for
|
|
|
demonstration::
|
|
|
|
|
|
import datetime
|
|
|
+ from django.utils import timezone
|
|
|
# ...
|
|
|
class Poll(models.Model):
|
|
|
# ...
|
|
|
- def was_published_today(self):
|
|
|
- return self.pub_date.date() == datetime.date.today()
|
|
|
+ def was_published_recently(self):
|
|
|
+ return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
|
|
|
|
|
|
-Note the addition of ``import datetime`` to reference Python's standard
|
|
|
-``datetime`` module.
|
|
|
+Note the addition of ``import datetime`` and ``from django.utils import
|
|
|
+timezone``, to reference Python's standard :mod:`datetime` module and Django's
|
|
|
+time zone-related utilities in :mod:`django.utils.timezone` respectively. If
|
|
|
+you aren't familiar with time zone handling in Python, you can learn more in
|
|
|
+the :doc:`time zone support docs </topics/i18n/timezones>`.
|
|
|
|
|
|
Save these changes and start a new Python interactive shell by running
|
|
|
``python manage.py shell`` again::
|
|
@@ -642,8 +658,8 @@ Save these changes and start a new Python interactive shell by running
|
|
|
>>> Poll.objects.filter(question__startswith='What')
|
|
|
[<Poll: What's up?>]
|
|
|
|
|
|
- # Get the poll whose year is 2007.
|
|
|
- >>> Poll.objects.get(pub_date__year=2007)
|
|
|
+ # Get the poll whose year is 2012.
|
|
|
+ >>> Poll.objects.get(pub_date__year=2012)
|
|
|
<Poll: What's up?>
|
|
|
|
|
|
>>> Poll.objects.get(id=2)
|
|
@@ -659,8 +675,8 @@ Save these changes and start a new Python interactive shell by running
|
|
|
|
|
|
# Make sure our custom method worked.
|
|
|
>>> p = Poll.objects.get(pk=1)
|
|
|
- >>> p.was_published_today()
|
|
|
- False
|
|
|
+ >>> p.was_published_recently()
|
|
|
+ True
|
|
|
|
|
|
# Give the Poll a couple of Choices. The create call constructs a new
|
|
|
# choice object, does the INSERT statement, adds the choice to the set
|
|
@@ -693,8 +709,8 @@ Save these changes and start a new Python interactive shell by running
|
|
|
# The API automatically follows relationships as far as you need.
|
|
|
# Use double underscores to separate relationships.
|
|
|
# This works as many levels deep as you want; there's no limit.
|
|
|
- # Find all Choices for any poll whose pub_date is in 2007.
|
|
|
- >>> Choice.objects.filter(poll__pub_date__year=2007)
|
|
|
+ # Find all Choices for any poll whose pub_date is in 2012.
|
|
|
+ >>> Choice.objects.filter(poll__pub_date__year=2012)
|
|
|
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
|
|
|
|
|
|
# Let's delete one of the choices. Use delete() for that.
|