Thursday, September 18, 2008

Django From the Ground Up: Episodes 1 and 2

with host Eric Florenzano

Bookmark and Share

Developing a complete web application takes a lot of work! It requires at least a passing knowledge of lot of different technologies--like Python, JavaScript, HTML, CSS, Linux, Apache, Nginx, Git, etc. Sometimes it's easy to see the big picture, but it's just as easy to get bogged down by the multitudes of details.

If you're anything like me, a good example can clear up a lot of confusion and make things a lot easier. There are a lot of examples online of how to do certain things with Django, Python, CSS, HTML, and other technologies, but there don't seem to be many examples that combine those many smaller examples into a complete package.

In this series, "Django From the Ground Up" I seek to change that - from setting up version control, building the models, views, and crafting the templates, to setting up and automatically deploying to a production server, Django From the Ground Up takes you every step along the way.

Before watching the screencasts (downloads located below), take note that the resulting site that you will build is already up and running online. It's called StartTheDark, and it's a site that lets you extremely easily plan out your night with your friends. The complete source code, including all of the media, is available on GitHub. If you're a tinkerer who likes reading through source code, there may be some hidden tips and tricks in there that we just don't have time to cover in the screencasts. All of these links can also be found at the open source page on StartTheDark itself.

Episode 1 - Setting Up Version Control

We start out the screencast with a small introduction to StartTheDark itself. After that, we get started with setting up version control. I think that it's important to set up some sort of version control for any project larger than just a few lines of code (and probably for those that are smaller, too). One reason is simply because you can keep track of your changes and back up if you make a mistake. Another reason to use version control, however, is because it makes it significantly easier to deploy. We'll cover this in depth later on in the screencasts, but doing a simple git fetch is much easier than copying a bunch of files around.

Why Git?

We could have just as easily used Subversion, Mercurial, Bazaar, or even CVS. But we had to make a choice, and since I personally prefer Git to the other options on this list, that's what I ended up using. It shouldn't matter which version control software you're using--you may need to tweak a few of the commands, but you should be able to follow along nicely. One of the great advantages that Git has right now, is the fantastic GitHub social code hosting platform. (Yes, there are competitors, but GitHub has more people and generally seemed to be the first to Get It Right.) You are strongly encouraged to fork the StartTheDark code tree and replace parts with your own code, improve it, and take it in new directions. I can't wait to see what people end up doing with the code!

Links for Learning Git

There are so many beginner's guides for Git that it really doesn't make sense to duplicate all of that great material here. The basic commands are used in the screencast, but I've found these Git references helpful as well:

If you have $9 to spare, the PeepCode Git Screencast is supposed to be one of the best ways to learn Git.

Episode 2 - Settings and Models

In this episode, we start by blazing through a bunch of changes to the settings file. Let's step back and go through them one by one.

Changes to settings.py

Inserting this snippet will be the base for us to use relative paths and have them programmatically adjusted into absolute paths. (The Django settings module requires absolute paths for most of its values.):

import os.path
PROJECT_DIR = os.path.dirname(__file__)

Filling in your name and e-mail under ADMINS will tell Django who to alert when an exception is thrown on a production site. This is a very handy way to tell when something has gone awry. (If you're running a larger site, it's probably a good idea to set up some more fine-grained monitoring systems, but in a pinch this will do.)

Setting DATABASE_ENGINE and DATABASE_NAME enables communication between Django and a database. We choose sqlite3 here because it's almost universally installed among Python installations (it's actually included in Python 2.5 and greater), and because it contains all of our database into a single file that we can control.

Setting MEDIA_ROOT allows for a standardized directory where Django can look for media files. Files that are uploaded through Django, for example, will end up somewhere under this directory.

MEDIA_URL then gives us a way to reference those media files. Django has no idea how we are serving those files. It could be from within Django itself, a separate process on the same server, or even on a different server entirely. Setting our URL this way allows for the greatest amount of flexibility.

LOGIN_REDIRECT_URL tells the django.contrib.auth files which URL to redirect the user to after they log in. This can be overridden by using a POST parameter (which we will use later), but the auth application defaults to /accounts/login/ which, in my experience, is almost never where you want the user to go after login.

Changing ADMIN_MEDIA_PREFIX is an almost completely aesthetic choice. It does make some practical sense as well, because it should really be a subdirectory underneath our media directory.

Appending the templates directory under TEMPLATE_DIRS allows us to begin writing templates and using them in our views.

Adding django.contrib.admin to INSTALLED_APPS means that we can use Django's automatic administration application to almost instantly work with our models.

That looks like a long list, but it takes around two minutes (with explanation) to actually perform all of those changes. There are quite a few settings available for modification so that you can bend Django to your every will. A list of all those settings can be found in the documentation.

Other Links for Episode 2

Here are a few links that should help boost your understanding of the episode:

  • Setting up a Django Development Environment: My first screencast which explains how to set up a development environment. This is useful to understand how everything works together behind the scenes in this screencast.
  • Overriding predefined model methods: Documentation explaining how and why you would want to override predefined model methods like save, or delete. We do this in the screencast, but this is some good background information. Note that the documentation explicitly takes in force_insert and force_update keyword arguments, whereas in the screencast we do use the **kwargs variable length argument list. For more information on variable length argument lists, see How to use *args and **kwargs in Python.
  • What happens when you save?: An excellent writeup of the series of events that happens when calling my_model.save().

Conclusions

We've only just begun! If you check out the StartTheDark code tree, you'll see pretty quickly that there is a lot of ground to be covered. I really hope that you enjoy these first two screencasts, as well as the rest to come. Look forward to another installment of Django From the Ground Up each day over the next few weeks. Let me know if there's anything that I should do differently, and please leave comments and questions below.

Comments - 12 people have already said something. Join the discussion.

  • Yashh said

    Eric you rock. Git rocks....

  • David, biologeek said

    Now Mercurial has bitbucket.org and it rocks too!</end of dcvs war>

  • Brajeshwar said

    This is awesome. Would be a definite reference for new Django Devs.

  • J. Pablo Fernández said

    Why not:

    django-admin.py startproject startthedark
    cd startthedark
    git init
    git add .
    git commit -m "Initial Django project."

    and then you can copy it to the server, clone it from the server, or whatever. There's no need to start the repository on the server (it's not SVN) and play with README files and _tmp directories.

  • Stefan said

    Thank you for this screencast, Eric! I'm looking forward to learn some extra tricks and tips in this series of screencasts.

    A good thing is to export the repository from local machine after done `git init` with:

    `git clone --bare -l .git ~/startthedark.git`. Then putting this on a server and cloning from there to your different locations.

  • Michael said

    I think 190MB for a 11 minute video is ridiculous. Please encode your quicktime movies into something smaller.

  • AJ said

    nice episodes...
    but 187MB? are you kidding me?

  • kevin said

    Thanks for the feedback everyone. We'll try and get these file sizes compressed a little for you shortly.

    And great job Eric. These series is going to be amazing.

  • Jonathan said

    Is this part of the Django Screencasts series? I guess my real question is: will this show up in the iTunes Podcast?

  • Empty said

    Jonathan: yes eventually. We're still working through all that integration.

  • UloPe said

    Good introduction.

    A little addition though:
    I have found that its better to use
    PROJECT_DIR = os.path.normpath(os.path.dirname(__file__))

    This way you avoid "ugly" paths (e.g. some/dir/../dir/) that could otherwise appear in some situations.

  • bts165 said

    Please explain the terms you use? Like what is relative path and absolute path?



    Beginner. Non programmer here hoping to learn more about web development, logic and codes. First heard about Django on youtube.



    How can you remember and apply all the “jargon” and syntax, many of which makes no sense to me and it never sticks. I guess if the designer of the coding bible can explain the logic behind a software, it helps.



    Also how did programmers learn logic and what finally make you say ” It clicks” now. It is very hard for me but I want to know.



    I only want to read without doing programming like a real programmer does but would love to help on snippets or repetitive task that programmer does not enjoy doing. Is there a great mentor available. Mentor commitment- 1 hour in any month of this year or as many minutes or as often you like.



    Contact:

    bts165 with yahoo dot com with subject line: Django.Programming.Logic.

    IM me at iamokyouareok with hotmail dot com

Leave a comment

Please leave your comments below.