We think its best to get a running start. The details and extent of the Django framework will be fleshed out in the later chapters, but for now, trust us, this chapter will be fun.
Installing Django is easy. Because Django runs anywhere Python does, Django can be configured in many ways. We cover the common scenarios for Django installations in this chapter. Chapter 20 covers deploying Django to production.
Django is written in 100% pure Python code, so youll need to install Python on your system. Django requires Python 2.3 or higher.
If youre on Linux or Mac OS X, you probably already have Python installed. Type python at a command prompt (or in Terminal, in OS X). If you see something like this, then Python is installed:
Python 2.4.1 (#2, Mar 31 2005, 00:05:10) [GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Otherwise, if you see an error such as "command not found" , youll have to download and install Python. See http://www.python.org/download/ to get started. The installation is fast and easy.
In this section, we cover two installation options: installing an official release and installing from Subversion.
Most people will want to install the latest official release from http://www.djangoproject.com/download/. Django uses the standard Python distutils installation method, which in Linux land looks like this:
Download the tarball, which will be named something like Django-0.96.tar.gz .
tar xzvf Django-*.tar.gz .
cd Django-* .
sudo python setup.py install .
On Windows, we recommend using 7-Zip to handle all manner of compressed files, including .tar.gz . You can download 7-Zip from http://www.djangoproject.com/r/7zip/.
Change into some other directory and start python . If everything worked, you should be able to import the module django :
>>> import django >>> django.VERSION (0, 96, None)
Note
The Python interactive interpreter is a command-line program that lets you write a Python program interactively. To start it, just run the command python at the command line. Throughout this book, we feature example Python code thats printed as if its being entered in the interactive interpreter. The triple greater-than signs (>>> ) signify a Python prompt.
If you want to work on the bleeding edge, or if you want to contribute code to Django itself, you should install Django from its Subversion repository.
Subversion is a free, open source revision-control system similar to CVS, and the Django team uses it to manage changes to the Django codebase. You can use a Subversion client to grab the very latest Django source code and, at any given time, you can update your local version of the Django code, known as your local checkout , to get the latest changes and improvements made by Django developers.
The latest and greatest Django development code is referred to as the trunk . The Django team runs production sites on trunk and strives to keep it stable.
To grab the latest Django trunk, follow these steps:
Make sure you have a Subversion client installed. You can get the software free from http://subversion.tigris.org/, and you can find excellent documentation at http://svnbook.red-bean.com/.
Check out the trunk using the command svn co http://code.djangoproject.com/svn/django/trunk djtrunk .
Create site-packages/django.pth and add the djtrunk directory to it, or update your PYTHONPATH to point to djtrunk .
Place djtrunk/django/bin on your system PATH. This directory includes management utilities such as django-admin.py .
Tip:
If .pth files are new to you, you can learn more about them at http://www.djangoproject.com/r/python/site-module/.
After downloading from Subversion and following the preceding steps, theres no need to python setup.py install youve just done the work by hand!
Because the Django trunk changes often with bug fixes and feature additions, youll probably want to update it every once in a while or hourly, if youre really obsessed. To update the code, just run the command svn update from within the djtrunk directory. When you run that command, Subversion will contact `http://code.djangoproject.com`_, determine if any code has changed, and update your local version of the code with any changes that have been made since you last updated. Its quite slick.
Djangos only prerequisite is a working installation of Python. However, this book focuses on one of Djangos sweet spots, which is developing database-backed Web sites, so youll need to install a database server of some sort, for storing your data.
If you just want to get started playing with Django, skip ahead to the Starting a Project sectionbut trust us, youll want to install a database eventually. All of the examples in the book assume you have a database set up.
As of the time of this writing, Django supports three database engines:
PostgreSQL (http://www.postgresql.org/)
SQLite 3 (http://www.sqlite.org/)
MySQL (http://www.mysql.com/)
Work is in progress to support Microsoft SQL Server and Oracle. The Django Web site will always have the latest information about supported databases.
Were quite fond of PostgreSQL ourselves, for reasons outside the scope of this book, so we mention it first. However, all the engines listed here will work equally well with Django.
SQLite deserves special notice as a development tool. Its an extremely simple in-process database engine that doesnt require any sort of server setup or configuration. Its by far the easiest to set up if you just want to play around with Django, and its even included in the standard library of Python 2.5.
On Windows, obtaining database driver binaries is sometimes an involved process. Since youre just getting started with Django, we recommend using Python 2.5 and its built-in support for SQLite. Compiling driver binaries is a downer.
If youre using PostgreSQL, youll need the psycopg package available from http://www.djangoproject.com/r/python-pgsql/. Take note of whether youre using version 1 or 2; youll need this information later.
If youre using PostgreSQL on Windows, you can find precompiled binaries of psycopg at http://www.djangoproject.com/r/python-pgsql/windows/.
If youre using a Python version over 2.5, you already have SQLite. If youre working with Python 2.4 or older, youll need SQLite 3 not version 2from http://www.djangoproject.com/r/sqlite/ and the pysqlite package from http://www.djangoproject.com/r/python-sqlite/. Make sure you have pysqlite version 2.0.3 or higher.
On Windows, you can skip installing the separate SQLite binaries, since theyre statically linked into the pysqlite binaries.
Django requires MySQL 4.0 or above; the 3.x versions dont support nested subqueries and some other fairly standard SQL statements. Youll also need the MySQLdb package from http://www.djangoproject.com/r/python-mysql/.
As mentioned earlier, Django doesnt actually require a database. If you just want to use it to serve dynamic pages that dont hit a database, thats perfectly fine.
With that said, bear in mind that some of the extra tools bundled with Django do require a database, so if you choose not to use a database, youll miss out on those features. (We highlight these features throughout this book.)
A project is a collection of settings for an instance of Django, including database configuration, Django-specific options, and application-specific settings.
If this is your first time using Django, youll have to take care of some initial setup. Create a new directory to start working in, perhaps something like /home/username/djcode/ , and change into that directory.
Note
django-admin.py should be on your system path if you installed Django via its setup.py utility. If you checked out from Subversion, you can find it in djtrunk/django/bin . Since youll be using django-admin.py often, consider adding it to your path. On Unix, you can do so by symlinking from /usr/local/bin , using a command such as sudo ln -s /path/to/django/bin/django-admin.py /usr/local/bin/django-admin.py . On Windows, youll need to update your PATH environment variable.
Run the command django-admin.py startproject mysite to create a mysite directory in your current directory.
Lets look at what startproject created:
mysite/ __init__.py manage.py settings.py urls.py
These files are as follows:
__init__.py : A file required for Python treat the directory as a package (i.e., a group of modules)
manage.py : A command-line utility that lets you interact with this Django project in various ways
settings.py : Settings/configuration for this Django project
urls.py : The URL declarations for this Django project; a table of contents of your Django-powered site
Where Should This Directory Live?
If your background is in PHP, youre probably used to putting code under the Web servers document root (in a place such as /var/www ). With Django, you dont do that. Its not a good idea to put any of this Python code within your Web servers document root, because in doing so you risk the possibility that people will be able to view your code over the Web. Thats not good for security.
Put your code in some directory outside of the document root.
Django includes a built-in, lightweight Web server you can use while developing your site. Weve included this server so you can develop your site rapidly, without having to deal with configuring your production Web server (e.g., Apache) until youre ready for production. This development server watches your code for changes and automatically reloads, helping you make many rapid changes to your project without needing to restart anything.
Change into the mysite directory, if you havent already, and run the command python manage.py runserver . Youll see something like this:
Validating models... 0 errors found. Django version 1.0, using settings 'mysite.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
Although the development server is extremely nice for, well, development, resist the temptation to use this server in anything resembling a production environment. The development server can handle only a single request at a time reliably, and it has not gone through a security audit of any sort. When the time comes to launch your site, see Chapter 20 for information on how to deploy Django.
Changing the Host or the Port
By default, the runserver command starts the development server on port 8000, listening only for local connections. If you want to change the servers port, pass it as a command-line argument:
python manage.py runserver 8080
You can also change the IP address that the server listens on. This is especially helpful if youd like to share a development site with other developers. The following:
python manage.py runserver 0.0.0.0:8080
will make Django listen on any network interface, thus allowing other computers to connect to the development server.
Now that the servers running, visit http://127.0.0.1:8000/ with your Web browser. Youll see a Welcome to Django page shaded a pleasant pastel blue. It worked!
Now that you have everything installed and the development server running, in the next chapter youll write some basic code that demonstrates how to serve Web pages using Django.
鍏充簬鏈瘎娉ㄧ郴缁
鏈珯浣跨敤涓婁笅鏂囧叧鑱旂殑璇勬敞绯荤粺鏉ユ敹闆嗗弽棣堜俊鎭備笉鍚屼簬涓鑸鏁寸珷鍋氳瘎娉ㄧ殑鍋氭硶锛 鎴戜滑鍏佽浣犲姣忎竴涓嫭绔嬬殑鈥滄枃鏈潡鈥濆仛璇勬敞銆備竴涓滄枃鏈潡鈥濈湅璧锋潵鏄繖鏍风殑锛
涓涓滄枃鏈潡鈥濇槸涓涓钀斤紝涓涓垪琛ㄩ」锛屼竴娈典唬鐮侊紝鎴栬呭叾浠栦竴灏忔鍐呭銆 浣犻変腑瀹冧細楂樹寒搴︽樉绀:
瑕佸鏂囨湰鍧楀仛璇勬敞锛屼綘鍙渶瑕佺偣鍑诲畠鏃佽竟鐨勬爣璇嗗潡:
鎴戜滑浼氫粩缁嗛槄璇绘瘡涓瘎璁猴紝濡傛灉鍙兘鐨勮瘽鎴戜滑涔熶細鎶婅瘎娉ㄨ冭檻鍒版湭鏉ョ殑鐗堟湰涓幓:
濡傛灉浣犳効鎰忎綘鐨勮瘎娉ㄨ閲囩敤锛岃纭繚鐣欎笅浣犵殑鍏ㄥ悕 (娉ㄦ剰涓嶆槸鏄电О鎴栫畝绉帮級
Many, many thanks to Jack Slocum; the inspiration and much of the code for the comment system comes from Jack's blog, and this site couldn't have been built without his wonderful
YAHOO.ext
library. Thanks also to Yahoo for YUI itself.