Tutorial: Setting up the database
Posted: March 16th, 2010 | Author: Adam | Filed under: Django, Python, programming | No Comments »So far we’ve started our project in our tutorial but, it really doesn’t do that much. The heart of most web applications is the database. This is where we will store all of the data for our application, in this case our blog posts for our blog.
In the main directory of our project (blog/) we have a file called settings.py. This is where all of the database settings are included. Django currently has built in support for postgre, mysql, sqlite3, oracle, mssql out of the box and you can use any database you’d like for this demo. It doesn’t matter. I’m going to be using sqlite since it’s very easy to use and requires very little configuration. When you open settings.py your find a section with DATABASE_ options. This is where we’ll start making changes
DATABASE_ENGINE = 'sqlite3' DATABASE_NAME = './blog.db' DATABASE_USER = '' DATABASE_PASSWORD = '' DATABASE_HOST = '' DATABASE_PORT = ''
As mentioned above you can use any database system you like.
Now let’s scroll down just a bit to the INSTALLED_APPS section of settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
)
These are the “apps” that are installed by default in every Django application. Each one does something to help us out in our application.
- auth - is a basic authentication framework for user authentication
- contenttypes - is a base class for a lot of other features in Django
- sessions - this is a basic session framework for users
- sites - this won’t be used in our demo but, it can allow you to have different domains running different websites work off the same code base and database
We are going to add two lines to this tuple here. The first line is “django.contrib.admin” and then “main” so our new INSTALLED APPS looks like this
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'main',
)
The first line will add the Django Admin interface to our application. This is by far one of the best features in Django. You might ask why this isn’t included by default in the list of installed apps like the others. The reason for this is that Django doesn’t want to do everything for you. This actually makes things more complex in the long run. If you need something you import it and if you don’t need it you’ll never see it. You will probably add the admin interface for every application you write though.
The second line adds our application “main” to the project so that Django knows to include it when we sync the database, which we’ll do right now. At the command line in the project root (blog/) directory run the command “python manage.py syncdb”
$ python manage.py syncdb Creating table auth_permission Creating table auth_group Creating table auth_user Creating table auth_message Creating table django_content_type Creating table django_session Creating table django_site Creating table django_admin_log
You just installed Django’s auth system, which means you don’t have any superusers defined.
Would you like to create one now? (yes/no):
Woah! What is all of this? Django is putting some basic tables into our database for the app’s that we have in the INSTALLED_APPS section of settings.py. We now have to answer a few basic questions on who the superuser for this application is. Go ahead and fill it out, it’s pretty easy
You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no): yes Username (Leave blank to use 'adam'): DeGizmo E-mail address: adam@degizmo.com Password: Password (again): Superuser created successfully. Installing index for auth.Permission model Installing index for auth.Message model Installing index for admin.LogEntry model
Django has created our superuser account and setup the basic permissions for this account. Your database is completly setup and ready to use. The next thing we need to is setting up the admin interface in step 4
Leave a Reply