Tutorial: Writing our first views

Posted: March 21st, 2010 | Author: | Filed under: Django, programming, Python | No Comments »

So far in the tutorial we’ve started our projectsetup the databaseenabled the admin interfacecreated models for our blog, and we just did a brief tutorial on Django’s ORM and DB-API. Now let’s look at getting data out of our database and onto the web page using views.

This section will be a bit short because we won’t really be using templates, which we’ll cover in the next section. We will just be looking at how the urls.py file and the views.py file interact and how we can use what we learned in our ORM tutorial not in the shell but, in our application.

Read the rest of this entry »


Tutorial: Learning the DB-API and ORM

Posted: March 20th, 2010 | Author: | Filed under: Django, programming, Python | 28 Comments »

So far in the tutorial we’ve started our projectsetup the database, enabled the admin interfacecreated models for our blog. We’re going to slow down on actually developing our blog and talk about exactly how we get to the data we’ve stored in the database so far. The Admin Interface is great but, it doesn’t provide good programtic access to the data. Let’s open up a python shell but, we’re going to do it a special way when working with Django. From the command line in our root project directory (blog/)

$ python manage.py shell
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>

Now this might look like a normal Python shell because, well it is. We’ve just loaded up some stuff in the background like path information to make our lives a bit easier. Now we have two models that we created in the application “main”. Let’s load those up in the shell.

Read the rest of this entry »


Tutorial: Create our models

Posted: March 18th, 2010 | Author: | Filed under: Django, programming, Python | No Comments »

So far in the tutorial we’ve started our projectsetup the databaseenabled the admin interface and now we’re ready to start to define the database for our project, a simple blog.

This is where we will start working with the models.py file in the “main” application directory (blog/main/). The models.py file is a definition file where we tell Django what all of our database models are. Let’s start with some categories for our blog entry.

from django.db import models
from django.contrib import admin

class Category(models.Model):
    Name    =   models.CharField(max_length=255)
    def __unicode__(self):
        return self.Name
admin.site.register(Category)

Let’s walk through this real quick since this was a lot of code real quickly. The first two lines we are first importing Django’s base class “models”. This is what we’ll subclass all of our models off of. The next line imports the Django admin object which we will use to include our model in Django’s admin interface.

Read the rest of this entry »


Tutorial: The Admin Interface

Posted: March 17th, 2010 | Author: | Filed under: Django, programming, Python | No Comments »

So we’ve setup our applicationinitilized our database and now we’re ready to take our first look at one of the cooler features in Django, the admin interface.

Let’s open up the urls.py file in the root project (blog/) directory. I’ve removed the some of the commented out lines below for brevity

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',

    # Uncomment the next line to enable the admin:
    # (r'^admin/', include(admin.site.urls)),
)<

Read the rest of this entry »


Tutorial: Setting up the database

Posted: March 16th, 2010 | Author: | Filed under: Django, programming, Python | 1 Comment »

So far we’ve started our project in our tutorial but, it really doesn’t do that much. The heart of most web applications is the database. This is where we will store all of the data for our application, in this case our blog posts for our blog.

In the main directory of our project (blog/) we have a file called settings.py. This is where all of the database settings are included. Django currently has built in support for postgre, mysql, sqlite3, oracle, mssql out of the box and you can use any database you’d like for this demo. It doesn’t matter. I’m going to be using sqlite since it’s very easy to use and requires very little configuration. When you open settings.py your find a section with DATABASE_ options. This is where we’ll start making changes

DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = './blog.db'
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = ''
DATABASE_PORT = ''

Read the rest of this entry »


Tutorial: Starting a Project

Posted: March 15th, 2010 | Author: | Filed under: Django, programming, Python | No Comments »

So now that you’ve completed the first part of the tutorial lets actually get started working with Django.

Let’s create folder called “django-projects” and from the command line in that folder

django-admin.py startproject blog

This command uses the built in django-admin command which creates a basic file structure for our project in a directory called “blog”. Let’s take a quick glance at the files that it made for us

  • __init__.py - This file is a “magic” python file that we can ignore
  • manage.py - This main file for working with your project from the command line. You won’t need to edit this file ever but, you’ll be using it a bit in a few minutes to run a development server, and sync up your database.
  • settings.py - Your basic config file. Contains your DB connection data, directory to your templates, what apps and extra Django modules you want the framework to load
  • urls.py - This is where we’ll define the URL’s (URI’s) for our application in a minute

This is the backbone for our project. Django define’s a project as a wrapper around many applications. So a single project can have lots of little applications inside of it. Right now we only have a project. Let’s create out application, again from the command line inside of the “blog” project directory

Read the rest of this entry »


Tutorial: Prerequisites and Installation

Posted: March 15th, 2010 | Author: | Filed under: Django, programming, Python | 1 Comment »

This tutorial is going to be strictly about Django. There are a few prerequisites you should be familiar with before going on.

  • Python
    Django is a web framework written in Python and you really need to know Python to use Django to it’s fullest potential. I highly recommend reading Dive Into Python, a free web based book, as a good primer to the language. I have an outline for an article on learning just enough Python to start with Django but, it’s not completed yet.
  • MVC
    Model / View / Controller concept of web frameworks. Arguably Django calls itself a MTV (Model / Template / View) Framework, the core concepts of separation of logic from presentation are very important for properly using Django. Knowing everything about MVC’s isn’t fully required but, if your completely unfamiliar with the concepts this tutorial does not teach them so you may get confused at the layout and structure of the program. This is especially true if your coming from a PHP background and were not using a framework before.

Read the rest of this entry »