Wednesday, January 4, 2017

Django - 003 - Django Settings - settings.py

Now, let us have a look at myD3site/settings.py. It is a normal python module with module-level variables representing Django settings.

Step 1: Let us set the TIME_ZONE to our time zone. We can find the list of timezones in the tz database here : https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

In Python, we can find the timezone for India:

>>> import pytz
>>> pytz.all_timezones[278]
'Asia/Kolkata'
>>>

Hence, we are setting the django timezone as follows:

TIME_ZONE = 'Asia/Kolkata'

Step 2: Note the INSTALLED_APPS setting in the file. This 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, INSTALLED_APPS contains the following apps, all of which come with django:

django.contrib.admin - The admin site
django.contrib.auth - An authentication system
django.contrib.contenttypes - A framework for content types
django.contrib.sessions - A session framework
django.contrib.messages - A messaging framework
django.contrib.staticfiles - A framework for managing staticfiles.

These applications are included by default as a convenience for the common case. Some of these applications make use of atleast one database table though, so we need to create the tables in the database before we can use them. To do that, run the following command:

~/Documents/my_d3_project/django/myD3site $ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK
~/Documents/my_d3_project/django/myD3site $ 


The migrate command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your settings.py file and the database migrations shipped with the app. You will see a message for each migration it applies.

Let us check the files in the outer myD3site directory:

~/Documents/my_d3_project/django/myD3site $ ls -lrt
total 44
-rwxr-xr-x 1 ubuntu ubuntu   806 Jan  4 18:17 manage.py
drwxr-xr-x 2 ubuntu ubuntu  4096 Jan  4 19:47 myD3site
-rw-r--r-- 1 ubuntu ubuntu 36864 Jan  4 19:47 db.sqlite3
~/Documents/my_d3_project/django/myD3site $

We can see that migrate command has also installed a sqlite database for storing database info!

No comments:

Post a Comment