Anatomy of a Django Project

We have version control (Subversion), database (PostgreSQL), language (Python), a framework (Django) and an IDE (SPE IDE). If you do not you can just read some of my previous posts to see how we set this all up.

Moving forward though, today I thought I would look at the anatomy of a Django project and how to set one up. You can find a very useful tutorial on the Django website where they step you through all the steps to get a simple voting application going.

Basically you have a Django project that houses multiple applications within it to create one solution. That’s what makes the whole Django Snippets community so awesome. People write applications and share it for you to use in your project. Like user registration confirmation or tagging or voting…

Let’s get started with getting our project up.

Open a terminal window and go to the directory that you want your project to reside in and type the following command:

$ django-admin.py startproject mysite

Obviously you can change “mysite” to anything that you want, something relevant to what your project is about :) You will notice that “startproject” created a directory named “mysite” with the following files:

  1. __init__.py: An file that simply tells Python that this directory should be considered a Python package.
  2. manage.py: A command-line utility that lets you interact with your Django project in various ways.
  3. settings.py: Settings file for your newly created Django project.
  4. urls.py: The URL declarations for your Django project.

One of the cool things that comes with the Django Framework is a development web server. So you do not need to setup any web servers to code your project, you can do that all later when you either have the time, inclination or the need to.

To run the development server simply type the following commands in a Terminal window (note that you must be in the directory of the your newly created project):

$ manage.py runserver

It will output some text stating that it has now created the development server. It defaults the port to 8000 (http://localhost:8000/). You can change this by adding the port you want to use in the arguments like so:

$ manage.py runserver 8080

You would then obviously go to http://localhost:8080/ in your web browser. You should see a nice message about it working :) This message shows because you have the “DEBUG” setting in the “settings.py” file set to “True”. You will set it to “False” when you want to actually run the project.

On to creating applications for our project to use.

To create an application you can simply type the following command in a Terminal window.

$ manage.py startapp myapp

You can yet again obviously change “myapp” to anything you want. You will notice however that another directory was created with your project’s directory called “myapp”. Again you will notice the following files were generated:

  1. __init__.py
  2. models.py: This file is used to define what the data in the database will look like. Basically you would define the data tables in this file which Django will later use to create the tables in your database.
  3. views.py: Basically a view is a web page that serves a specific method and has a specific template.

Once your Models are defined in your application you can activate them by adding a line to the “INSTALLED_APPS” setting in your “settings.py” file like so, ” ‘mysite.myapp’ “.

Once that is done and saved type the following command in a Terminal window:

$ manage.py syncdb

This will connect to the database with the credentials given in “settings.py” and create all the necessary tables etc.

Just a little side note. Another awesome feature of Django is that it also generates an Admin section of your web project that you can use to administer the users, groups, and anything to do with your applications within the project.

That is as much as I am going to show you today as I just wanted to have a look at how it all fits together rather than creating a complete solution. I for one need to get better acquainted with Python before attempting an entire web solution.

If you would like to continue please complete the tutorial on the Django website. Below are some more useful links to get you going.

  1. http://www.djangoproject.com/
  2. http://www.djangosnippets.org/
  3. http://www.djangobook.com/
  4. http://www.b-list.org/weblog/categories/django

Some videos:

  1. http://video.google.com/videoplay?docid=2939556954580527226
  2. http://video.google.com/videoplay?docid=921835378048178313
  3. http://video.google.com/videoplay?docid=-70449010942275062

Till next time :) If you have any other awesome links please post them here for me to check out. Thanks.

2 Responses to “Anatomy of a Django Project”

  1. [...] Anatomy of a Django Project We have version control (Subversion), database (PostgreSQL), language (Python), a framework (Django [...]

  2. Just wanted to note that for the aptitude (or apt-get or synaptic) install of Django on LinuxMint (and I’m assuming it’s the same for Ubuntu), I start a Django project with ‘django-admin startproject mysite’, and it chokes on ‘django-admin.py startproject mysite’… Knowing this from the outset would have saved me a small headache, so thought posting it might be useful for others :-) .


Leave a Reply