Skip navigation

Is really, really good. It’s a MVC web framework written in Python with content-management in mind. The most general unit in Django is a project, which is a collection of applications. Applications are just plugins, and can be used in one or more projects. Remember my previous post on adding a new component to DrProject? Well, with Django it’s a lot simpler, as I’ll show soon. Django supports PostgreSQL, MySQL, Sqlite 3, and Oracle. It also comes with database API, which is very similar to SQLAlchemy.

To create a new project, we can use django-admin.py to create directory structure so we don’t have to do it manually. When we invoke django-admin.py startproject <project_name>, Django will create the following directory tree:

<project_name>/
__init__.py
manage.py
settings.py
urls.py

For example, the settings.py file just contains setting fields (such as database-related stuff). The backend follows the DRY (Don’t Repeat Yourself) principle. This means that we define anything that we want to store in the database as a model. A model is essentially a class inheriting django.db.models.Model with fields, a concept similar to Elixir/SQLAlchemy. A model belongs to an application that uses that model. To create a new app in Django, we go to the directory in which the project was created and invoke python manage.py startapp <app_name>. This creates the following directory tree:

<app_name>/
__init__.py
models.py
views.py

This looks like MVC, right? In models.py we specify the models that we’ll want to store in the database. When we’ve added models to this file, we call python manage.py syncdb from the project root directory, which calls execute_manager from django.core.management with our settings, and applications. One little caviat – we need to manually add our application to INSTALLED_APPS tuple in the settings.py using its full name (<project_name>.<application_name>). When this command is called, Django will create any tables related to our models.

This completes the intro to creating projects/apps and defining models. Next, I’ll show how to add controllers to handle requests and templates to show shiny stuff.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: