Django Interviews

I completed the interviews of a lot of interesting people from the django community. Here are all the interviews, in reverse chronological order.

Adrian Holovaty

Jacon Kaplan Moss

Michael Trier

Russel Keith Magee

James Bennett

Malcolm Tredinnick

Interviews of Django people

I have started a series to interview the interesting people from Django community. You can read about them at

1. James Bennett

2. Malolm Trednnick

I have posted a few tutorials about using django with appengine

http://www.42topics.com/dumps/django/docs.html

http://www.42topics.com/dumps/appengine/doc.html

And here you can find other of my Django posts.

Day 3 - Writing the model

[Still working on this]

By now we have the model in place and we can use the admin interface to create or edit any of the tables. Use the admin interface to create a Blog and a Comment on that. The admin model is useful if you just want to do CRUD operations on the tables, but it does not show the data in the format we want. We would like to have each Blog entry displayed with its comments on the main page. We can not give the access to the admin interface, as we want visisters to just be able to read, and not write any thing. So we need views. We wrote a view on the first day when we wrote the welcome function.

The basic things a view function needs to do are

  1. Be mapped to a set of URLs by its being specified in urls.py
  2. Take a HtttpRequest object request as the first argument.
  3. Return a HttpResponse object.

Now we could write the html inside of the view function, but once you start building complex interfaces, generating Html becomes a chore. It is much easier to take a template and pass values which change to it. Django makes this extremely easy.

Day 2 - The basic model

Django provides a very nice ORM, which means you generally would not need to write database queries. IMO, Django’s well designed and very simple ORM is one of its strongest points. So lets dive in and start some code.

Before we start interacting with the database we need to tell Django the database connection details. This, and most other settings are defined in the file settings.py. For us that file is f:\project\settings.py. I am going to use the mySql database server, for no other reason than I already know this. You may want to use sqllite or PostgreSql.
Open the setting.py file and change the following lines to store the database details.

DATABASE_ENGINE = ‘mysql’ # ‘postgresql_psycopg2′, ‘postgresql’, ‘mysql’, ’sqlite3′ or ‘oracle’.
DATABASE_NAME = ‘dBlog’ # Or path to database file if using sqlite3.
DATABASE_USER = ‘user’ # Not used with sqlite3.
DATABASE_PASSWORD = ’secret’ # Not used with sqlite3.
DATABASE_HOST = ” # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ” # Set to empty string for default. Not used with sqlite3.

You would have need to created the user and the database, using the tool provided by your database engine. Django can not do this for you.

The admin system provided with Django lets you do most of the basic CRUD without writing a line of code. So lets use that to see our data. We need to tell Django that we plan to use the admin functionality. Modify the settings.py to do this.

INSTALLED_APPS = (
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.sites’,
‘django.contrib.admin’,
‘project.dBlog’
)

Basically we added ‘django.contrib.admin’ and ‘project.dBlog’ to INSTALLED_APPS.

Right now we just want to have blog posts and comment on them. Edit your models.py to create a table for blog entries and another for comments. Put the following code in models.py

from django.db import models
from django.contrib.auth.models import User

class Blog(models.Model):
entry = models.TextField(maxlength = 1000)
created_on = models.DateTimeField(auto_now_add = 1)
created_by = models.ForeignKey(User, unique = True)
class Admin:
pass

class Comment(models.Model):
comment_text = models.TextField(maxlength = 1000)
created_on = models.DateTimeField(auto_now_add = 1)
created_by = models.ForeignKey(User, unique = True)
comment_for = models.ForeignKey(Blog)
class Admin:
pass

Lets see what the code does.
The first line imports the module which contains django ORM. The next import imports User class which we would be using as we do not want to re write the authentication system. We have two classes Blog and Comment each corresponding to a database table. Such classes need to extend models.Model which we have done. We want the Blog table to contain some text, so we have entry, we want to keep track of when it was created, so we have created_on. created_by is a Foreign key to the User table, defined in django.contrib.auth.models . For each of them we have added a few attributes such as maxlength.

The code

class Admin:
pass

tell django that we want to modify these tables from the auto generated admin interface. Similarly we write the needed columns for Comment and give it a foreign key to Blog. (created_by = models.ForeignKey(User, unique = True))

Running the command manage.py syncdb would create the tables our application needs.

F:\project>manage.py syncdb
Creating table auth_message
Creating table auth_group
Creating table auth_user
Creating table auth_permission
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Creating table dBlog_blog
Creating table dBlog_comment
You just installed Django’s auth system, which means you don’t have any superuse
rs defined.
Would you like to create one now? (yes/no): yes
Username: shabda
E-mail address: shabda.raaj@gmail.com
Password:
Password (again):
Superuser created successfully.
Installing index for auth.Message model
Installing index for auth.Permission model
Installing index for admin.LogEntry model
Installing index for dBlog.Blog model
Installing index for dBlog.Comment model
Loading ‘initial_data’ fixtures…
No fixtures found.

We would be using Django’s authentication system, as declared in INSTALLED_APPS in setting.py, with the lines ‘django.contrib.auth’. So syncdb created a few tables for the authentication system and asked you to create a superuser.

To make the admin interface available we need to add

(r’^admin/’, include(’django.contrib.admin.urls’)),
to urls.py.

Now point your browser to localhost:8000/admin and you would be asked to log in. Log in using the user you created. You should see a screen like this .
Rejoice, our first app is almost ready.

Day 1 - Your first code

Before we get into the deep and the dirty of coding the Blog applications, lets check if the server parses the Python code. Modify the f:\project\urls.py to tell what function we want to be called when we access a page. Lets have a simple welcome page when we access /dBlog/


from django.conf.urls.defaults import *

urlpatterns = patterns(’dBlog.views’,
(r’^dBlog/$’, ‘welcome’),
)
The line from django.conf.urls.defaults import * imports the pattern function which we use here. The (r’^dBlog/$’, ‘welcome’), is the interesting line. This tell django that we want to call welcome function from module dBlog.views whenever localhost:8000/dBlog is accessed.

Modify the f:\project\dBlog\views and code the folowing lines

from django.http import HttpResponse

def welcome(request):
return HttpResponse(’Welcome to dBlog. We are building the worlds best blog engine here!’)

The function which gets called takes the first parameter containing the HTTP request data, (By convention called request). It must return a HttpResponse containing the text to write. (There are much more advanced ways to return HttpResponse, we will find them shortly). Now access the http://localhost:8000/dBlog/ . You would see the text which you returned from HttpResponse.

That is all for today. Tomorrow we will decide upon the data model, and write some more code.

Day 1 - The first steps.

I use windows as my development platform. By the way, you have installed Django, haven’t you. If not head over to installation guide and install. We want to start a Django project and then a Django application inside the project.

F:\>django-admin.py startproject project
F:\>cd project

F:\project>django-admin.py startapp dBlog
Lets see if everything got created correctly.
F:\project>cd dblog

F:\project\dBlog>dir
Volume in drive F is New Volume
Volume Serial Number is 5C42-C4C6

Directory of F:\project\dBlog

12/07/2007 06:35 PM .
12/07/2007 06:35 PM ..
12/07/2007 06:35 PM 60 models.py
12/07/2007 06:35 PM 27 views.py
12/07/2007 06:35 PM 0 __init__.py
3 File(s) 87 bytes
2 Dir(s) 32,769,191,936 bytes free

F:\project\dBlog>cd ..

F:\project>dir
Volume in drive F is New Volume
Volume Serial Number is 5C42-C4C6

Directory of F:\project

12/07/2007 06:35 PM .
12/07/2007 06:35 PM ..
12/07/2007 06:35 PM dBlog
12/07/2007 06:35 PM 557 manage.py
12/07/2007 06:35 PM 2,899 settings.py
12/07/2007 06:35 PM 236 urls.py
12/07/2007 06:35 PM 0 __init__.py
4 File(s) 3,692 bytes
3 Dir(s) 32,769,191,936 bytes free
Looks like django created some files and directories for its use. Never mind what all of that means. All those files will make sense shortly. (It is extremely simple, by the way).

Now lets try running the server and accessing the application we created.


F:\project>manage.py runserver
Validating models…
0 errors found

Django version 0.97-pre-SVN-6442, using settings ‘project.settings’
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Looks like the server is started on port 8000. Give it a spin.
It worked!

Aww. It says “Of course, you haven’t actually done any work yet.” I shall not let a computer mock me. So back to work.

Deciding on the features

So what do you want to be in your blog? Just the basics please. We will add features as we move along.
We want

  1. A list of all the pages
  2. A detail view of each post
  3. Ability for the blog owner to post
  4. Comments!

Lets get it done.

Building a Django blog.

Some days ago James Bennett asked “Where is Django’s blog application“. And he replies

there is no “definitive” Django blogging application; there are a bunch of them available if you go looking, but you’re not likely to get anyone to recommend one of them as “the” Django blogging app

Well, this is what we are going to build. The definitive django blogging application. In 14 days. In Django. Because we are perfectionists, because we have a deadline.