I’ve been using Django the past couple of months or so. Like Rails, it’s a “convention over configuration” web framework based on a popular scripting language (in this case Python). I like its flexibility and the fact that it’s easy to learn. It’s a little less “black box”-y than Rails, and as a Linux user, that’s right in my sweet spot.
I’ve decided to create a series of tutorials on the basics of Django. I can’t claim that they’re definitive, but they’re ways I’ve found to make Django work for me. They’ll be pretty basic, covering CRUD operations using a basic model. Because I’m in to music, it’ll be a song database, where you can add songs, list, search, and edit songs.
First, you’ll need to install Django. I use Ubuntu, so, if you do too, it’s in the repos. If not, go to the Django homepage and get it there according to instructions.
Next, assuming Django is all installed, let’s create our project. Open a terminal window, navigate to where you want to keep your project, and enter
django-admin.py startproject songs
(You may have to leave off the ‘.py’)
This creates a folder called “songs” and a few python files. cd into songs.
Open the settings.py–we need to set the sqlite database engine for our project. Look for the DATABASES dictionary defined, and on the line ‘ENGINE’: ‘django.db.backends.’ after the dot enter sqlite3. Then, for the NAME key, enter the full path to where your db file will reside.
Next, run the following at the command prompt:
python manage.py syncdb
This will set up the database, and allow you to create an admin name and password.
Finally, create our Songster app with the following:
python manage.py startapp songster
This will create another folder songster, and several more files.
We can see our brand new app in action. Start the server by entering the following at the command line (remember to be in the songs directory):
python manage.py runserver
Open a browser and browse to http://localhost:8000/. You should see an “It worked!” page in pastel blue.
That’s it for now! Next time, we’ll create a Song object (or model, in Django speak), use the django shell to set up a couple of songs, and develop our list functionality.