Home > Articles > Open Source > Python

Django for the Impatient: Building a Blog

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close WindowWesley Chun

Wesley ChunJeff ForcierPaul Bissex 

Learn more…

Sorry, this author hasn't written any articles.

Sorry, this author doesn't have anything for sale.

Sorry, this author hasn't posted any blogs.

Python Web Development with Django

This chapter is from the book
Python Web Development with Django

How fast can you produce a simple blog using Django? Pretty darn fast.

Django bills itself as “the Web framework for perfectionists with deadlines.” So let’s put ourselves on deadline and see how fast we can produce a simple blog using Django. (We’ll address your perfectionist side later.)

All the work in this chapter is done on the command line in your shell of choice (bash, tcsh, zsh, Cygwin, or what have you). So open your terminal and cd to a directory that is on your PYTHONPATH environment variable. On a Unix-based system such as Linux, Mac OS X, FreeBSD, and so on, you can issue an echo $PYTHONPATH command to see its contents; from a Win32 Command window, type echo %PYTHONPATH%. You can read more about paths in both the installation and Python chapters.

We recommend you try to follow along and actually build the blog as you go. If that’s not practical—if you aren’t near a computer, or you’re just impatient—simply reading it is illuminating too. That’s especially true if you have experience with one or more other modern Web frameworks, since many of the basic concepts are familiar.

If you are following along on your own computer, and you reach a point where the results you’re getting don’t match what you see here, stop and re-examine the step you just completed, and then review the two or three steps before that. Look for a spot where you could have skipped over a seemingly unimportant detail or didn’t understand a specific instruction. If no light bulbs come on, delete your sample project and start over. The authors used this method when learning Django; in addition to being faster than staring blankly at error messages for hours, the repetition of the steps leading up to your trouble spot really help with your retention!

Creating the Project

The easiest way to organize your Django code when you are starting out is to use what Django calls a project: A directory of files that constitute, usually, a single Web site. Django comes with a utility called django-admin.py to streamline tasks such as the creation of these project directories. On Unix, it has a default installation into the /usr/bin directory, and if you’re on Win32, it goes into the Scripts folder right in your Python installation, for example, C:\Python25\Scripts. In either case, you need to make sure that django-admin.py is in your PATH so it can be executed from the command line.

To create the project directory for your blog project, issue this django-admin.py command:

django-admin.py startproject mysite

On a Win32 box, you need to open a DOS Command window first. It can be accessed via Start -> Programs -> Accessories -> Command Prompt. Also, instead of a $, you see something like C:\WINDOWS\system32> as a shell prompt.

Now take a look at the contents of the directory to see what this command has created for you. It should look something like this on Unix:

$ cd mysite
$ ls -l
total 24
-rw-r--r--    1 pbx  pbx     0 Jun 26 18:51 __init__.py
-rwxr-xr-x    1 pbx  pbx   546 Jun 26 18:51 manage.py
-rw-r--r--    1 pbx  pbx  2925 Jun 26 18:51 settings.py
-rw-r--r--    1 pbx  pbx   227 Jun 26 18:51 urls.py

If you were developing on a Win32 platform, opening an Explorer window to that folder looks something like Figure 2.1, if we created a folder named C:\py\django with the intention of putting our project there.

Figure 2.1

Figure 2.1 mysite folder on Win32

Besides __init__.py, the startproject command has created three other files.

  • manage.py is a utility for working with this Django project. You can see from its permissions flags in the directory listing that it is executable. We run it in a moment.
  • settings.py is a file containing default settings for your project. These include database information, debugging flags, and other important variables. Any value in this file is available to any of your project’s installed apps—we show you the usefulness of that as we progress through this chapter.
  • urls.py is what’s known in Django as a URLconf, a configuration file that maps URL patterns to actions your applications perform. URLconfs are an exciting and powerful feature of Django.
  • Share ThisShare This
  • Your Account

Discussions

Impatient?
Posted Dec 1, 2008 10:52 AM by sorr53583
0 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

 Big Nerd RanchAsk Big Nerd Ranch: Blocks in Objective-C
By Big Nerd Ranch on June 24, 2010 No Comments

Adam Preble answers a question about blocks.

Danny KalevYves Smith: Suspicions that The Fed is manipulating Wall Street
By Danny Kalev on May 24, 2010 No Comments

Yves Smith, the nom de plume of the creator of Naked Capitalism and one of the most savvy and respected members of the blogosphere. In professional life Yves is known as Susan Webber. Yves recently gave an interview to an Israeli financial newspaper in which she claims that a federal team unofficially called "the plunge protection team" is manipulating the stocks on Wall Street.

 Big Nerd RanchAsk Big Nerd Ranch: Rotating an iPhone View Around a Point
By Big Nerd Ranch on May 20, 2010 No Comments

Brian Hardy answers a question about view rotation.

Sample code for this article is available in the Big Nerd Ranch github repository. The sample application demonstrates several techniques illustrated here, and works on iPhone or iPad.

Q. On iPhone OS, how can I rotate a view around an arbitrary point?

A. By default, views in Cocoa Touch (and Cocoa) are configured to rotate around their center point. While this is commonly useful (think of a UIActivityIndicatorView), often you will want to use a point other than the center. There are (at least) two ways of doing this. You can change the anchorPoint property of the view's layer. Alternatively, you can wrap the view in a superview, with the superview's center located at the point you want to rotate around. In either case, the mechanism for rotation is the same. Both techniques are discussed here.

See All Related Blogs

Informit Network