Django is a Python web framework. It is one of the most popular Python frameworks. You can find it HERE.

I wanted to take a look at Django for quite some time and today is the day!

To begin with Django you firstly need to download and install it. So…

How to download and install Django

Download it:

wget http://www.djangoproject.com/download/1.3/tarball/

Extract it:

tar xzvf Django-1.3.tar.gz

Open the folder and install it:

cd Django-1.3/
python setup.py build
python setup.py install

Now you should have Django installed. Next step would be to create a Django project.

How to start a Django project

Open the directory you want to create the project in and execute this command:

django-admin.py startproject myproject

Change into myproject directory and you should find a few files:

  • init.py

  • manage.py

  • settings.py

  • urls.py

To verify that it is in fact working, you need to fire it up. Django includes a simple web server written in Python. It is important: do not use Django built-in web server for production, use it for development ONLY! After all it’s only a framework. To launch the application use this command:

python manage.py runserver

Now if you open up http://127.0.0.1:8000/ in your browser, you should see a demo Django page. If you see it - congratulations, you’ve done everything right and you’re ready to go!

 Configuring the app

Every single application (or next to every single one) needs some changes to the default configuration. Modify settings.py file to configure the application. Since I’m going to take a look at database usage and I only have MySQL installed, let’s try using it. Set the database engine to django.db.backends.mysql and enter login details. You might need to install python-mysqldb package (if you’re using Linux like me). You can do that by executing:

sudo apt-get install python-mysqldb

After that run syncdb to add necessary tables:

python manage.py syncdb

If you don’t get any errors you’re all set to start coding!