This file is automatically generated with default values whenever you create a new project. Each project has its own settings.py file that controls that project.
When you install Python in your system, it comes with code editor called 'IDLE'. You can use this application to create a Python code or edit it.
So, open up the settings.py file using this 'IDLE' editor.
First of all, we shall start with setting up the database. Like explained in our earlier posts, Python comes with a in-built compact database called 'sqlite3'.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
This will use the 'sqlite3' database and the data is stored in 'db.sqlite3' file which will be in the same folder of 'mysite'.
If you want to use other database types instead of 'sqlite3', you can do so by changing the highlighted value accordingly as follows:
MySql - mysql
PostgreSql - postgresql_psycopg2
Oracle - oracle
And there can be other values to define the user name and password for connecting to the database as well as the port number as shown below:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Note that, for 'sqlite3' database type the database file will be created automatically whenever you need it. But for other databases like 'MySql' or 'PostgreSql' you need to create the database. We will cover more about that later.The 'TIME_ZONE' setting property is used to specify what time zone you belong to so that the correct date and time values are stored into the database.
By default it is set to 'UTC' format. You can set the value based on this list present here.
Likewise, you can set the language by changing the value for 'LANGUAGE_CODE' property. By default, it will be 'en-us'
The property 'INSTALLED_APPS' define the applications that have to be included in the project which contain those that come with Python and your own. It looks like below:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
- django.contrib.admin – The admin site. You’ll use it in part 2 of this tutorial.
- 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 static files.
python manage.py syncdb
This will check the values in 'INSTALLED_APPS' and creates the necessary tables and columns accordingly.
If you are running this command for the first time, it will prompt you for creating a user name and password for accessing the administrator web site. You can see the list of tables it is creating based on the values and settings in 'INSTALLED_APPS'.
It creates the database file 'db.sqlite3' in the 'mysite' folder. At the DOS prompt, type in 'dir' to get the list of files in the folder and check whether this file is present or not.
Now we are ready to create our first application.
No comments:
Post a Comment