Step 6: Set Up Database

Later on we'll use MySQL on your actual host server, but for development, we'll use sqlite3.  It should already be on your system, but maybe not on your file path.  Type the following in your terminal:

vernon@slick:~/project/zing$ sqlite3

You should see something like this:

SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

If nothing happens, lets just fix that the easy way:

vernon@slick:~/project/zing$ sudo apt-get install sqlite3

Done, good. 

Now, if you were scared off by the thought of the database, don't be.  All we have left to do is tell Django about our database, and we'll do that with the settings.py file. 

You can open the settings file from your Terminal again.  From now on I'll just say emacs, but you know that you can use whatever your favorite text editor is. 

vernon@slick:~/project/zing$ emacs settings.py

At first glance the settings.py file might look a little daunting, but you'll soon get use to it.  We are going to do a few things in here.  Don't be to worried about doing the wrong thing.  If it all goes wrong you can just delete the entire project and start from scratch. 

For now, lets edit a few things.  First up, scroll down to this section:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '', # Or path to database file if using sqlite3.
        'USER': '', # Not used with sqlite3.
        'PASSWORD': '', # Not used with sqlite3.
        'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '', # Set to empty string for default. Not used with sqlite3.
    }
}

Anything after a '#' in Python is a comment.  Read some of that and you'll see why we start with sqlite3.  Change it to this: 

DATABASES = {
    'default': {
        'ENGINE': 'sqlite3',
        'NAME': 'zing.db',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

That should be really easy.  Once we start working with MySQL it is a little more complex.  You can either delete or keep the comments.  It doesn't really matter. 

We specified two things, that we were going to use sqlite3 and the name.  Actually, in the case of sqlite3 we also specified the file path, and for now we are just going to have it right here, in the zing directory. 

While we are here, lets do something else.  Lets add our template directory.  Scroll down to where it says: TEMPLATE_DIRS = ( and change it like this:

TEMPLATE_DIRS = (
    '/home/username/project/zing/template'
)

If you are not familiar with Python, it is really important to keep the spaces.  Keep the indentation to four spaces.  If you don't understand what I am talking about, almost any introduction to Python will clear that up for you. 

We'll be back in the settings.py file later on, but for now we just want to change one last thing.  In the INSTALLED_APPS section (right at the bottom), change it like this (add the bit in red):

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    'zing.blog',
)

This is a Python tuple, and it is a good idea to put that comma at the end (in case you add more.) 

We have now told our project which database to use, where to find our HTML templates and which apps we have created. 

Close the text editor and go back to your Terminal again.  Do like so:

vernon@slick:~/project/zing$ python manage.py validate
0 errors found

If you get errors, you have done something wrong above.  Look through all the steps carefully, and if you can't figure it out, one drastic option is just to delete the whole thing and start again.  You can do this by going to the projects directory, and typing 'rm -r *'.  Then you'll have to start from the top again. 

I am going to assume all is well and we can carry on. 


Share |
« Previous Page  |  Return To Contents Page  |  Next Page »

 

Thank you for visiting

This is my personal blog, but I try to write things that you may find interesting or useful.  I'm currently working on a six month long project digging into the elements that make a blog meaningful. For more on that, visit MEANINGFUL BLOG PROJECT page

You may want to know a little about me personally, so start at the about page. 

If you find some of what is here interesting, or you want to follow the project through, why not subscribe.

Tweet