官术网_书友最值得收藏!

Installing the Required Software

Our development environment consists of Python, Django, and a database system. There are many different database systems available, but for the following examples, we will be using Sqlite3 which is included in the Python download. In this section, we will see how to install the necessary software packages.

Installing Python

Django is written in Python, so the the first step in setting up our development environment is to install Python. Python is available for a variety of operating systems, and installing Python is not very different from installing any other software package. The procedure depends on your operating system.

You will need a recent version of Python. Django requires Python 2.3 or higher. The latest version of Python at the time of writing is 2.5.

We will now describe the installation process for each operating system.

Installing Python on Windows

Python has a standard installer for Windows users. You will need to go to http://www.python.org/download/ and download the latest version. Next, double-click the .exe file and follow the installation instructions. The graphical installer will guide you through the installation process and create shortcuts to Python executables in the Start menu.

Once the installation has been done, we need to add the Python directory to the system path so that we can access Python while using the command prompt. To do this, open the Control Panel, double-click the System icon, go to the Advanced tab and click the Environment Variables button. A new dialog box will open. Select the Path system variable, and append the path where you installed Python. (The default path is usually c:\PythonXX, where XX is your Python version, but the folder is actually named Python25, so if your have Python version 2.5, you should name the command c:\Python25.) Don't forget to separate the new path from the one before it with a semicolon.

If you want to test your installation, open the Run dialog, type python and hit Enter. The Python interactive shell should open.

Installing Python on UNIX/Linux

If you use Linux or UNIX, chances are that you already have Python installed. To check, open a terminal, type python and hit Enter. If you see the Python interactive shell, then you already have Python installed:

Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

The first line of the output indicates the version installed on your system (2.5.1 here).

If you receive an error message instead of seeing the above output, or you have an old version of Python, you should read on.

UNIX users and Linux users are recommended to install and update Python through the system package manager. Although the actual details vary from system to system, it won't be any different from installing any other package.

For APT-based Linux distributions (such as Debian and Ubuntu), open a terminal and type:

$ sudo apt-get update
$ sudo apt-get install python

Or if you have the Synaptic package manager, simply search for Python, mark its package for installation, and click on Apply.

Users of other Linux distributions are recommended to check their system documentation for information on how to use the package manager to install packages.

Installing Python on Mac OS X

Mac OS X comes with Python pre-installed. However, due to Apple's release cycle, it's often an old version. If you start the Python interactive shell and find a version older than 2.3, you should visit this URL: http://www.python.org/download/mac/ and download the most recent installer for your version of Mac OS X.

Now that Python is up and running, we are almost ready. Next, we will install Django and make sure that we have a database system.

Installing Django

Installing Django is very easy, but it depends on your operating system. Since Python is a platform-independent language, Django has one package that works everywhere regardless of your operating system.

To download Django, head to http://www.djangoproject.com/download/, and grab the latest official version. The code in this book was developed on Django 0.96 (the latest version at the time of writing), but most of the code should run on later official releases. Next, follow the instructions related to your platform.

Installing Django on Windows

After you download the Django archive, extract it to the C drive, and open a command prompt (by clicking on Start then Accessories). Change the current directory to where you extracted Django by issuing the following command, where x.xx is your Django version:

c:\>cd c:\Django-x.xx

Next, install Django by running the following command (for which you will need administrative privileges):

c:\Django-x.xx>python setup.py install

If the above instructions do not work, you can manually copy the django folder in the archive to the Lib\site-packages folder located in the Python installation directory. This will do the job of running the setup.py install command.

Tip

If you do not have a program to handle .tar.gz files on your system, I recommend using 7-Zip, which is free and available at http://www.7-zip.org/.

The last step is copying the django-admin.py file from Django-x.xx\django\bin to somewhere in your system path, such as c:\windows or the folder where you installed Python.

Once this has been done, you can safely remove the c:\Django-x.xx folder, because it is no longer needed.

That's it. To test your installation, open a command prompt and type the following command:

c:\>django-admin.py --version

If you see the current version of Django printed on screen, then everything is set.

Installing Django on UNIX/Linux and Mac OS X

Installation instructions for all UNIX and Linux systems are the same. You need to run the following commands in the directory where the Django-x.xx.tar.gz archive is located. These commands will extract the archive and install Django for you:

$ tar xfz Django-x.xx.tar.gz
$ cd Django-x.xx
$ sudo python setup.py install

The above instructions should work on any UNIX or Linux system as well as on Mac OS X. However, it may be easier to install Django through your system's package manager if it has a package for Django. Ubuntu has one, so to install Django on Ubuntu, simply look for a package called python-django in Synaptic, or run the following command:

$ sudo apt-get install python-django

You can test your installation by running this command:

$ django-admin.py --version

If you see the current version of Django printed on screen, then everything is set.

Installing a Database System

While Django does not require a database for it to function, the application that we are going to develop does. So in the last step of software installation, we are going to make sure that we have a database system for handling our data.

It is worth noting that Django supports several database engines: MySQL, PostgreSQL, MS SQL Server, Oracle, and SQLite. Interestingly however, you only need to learn one API in order to use any of these database systems. This is possibly because of Django's database layer, which abstracts access to the database system. We will learn about this later, but for now you only need to know that, regardless of which database system you choose, you will be able to run the Django applications developed in this book (or elsewhere) without modification.

If you have Python 2.5 or higher, you won't need to install anything, since Python 2.5 comes with the SQLite database management system contained in a module named sqlite3. Unlike client-server database systems, SQLite does not require a resident process in memory, and it stores the database in a single file, which makes it ideal for our development environment. Therefore, throughout this book, we will be using SQLite in our examples. Of course you are free to use your preferred database management system. We can tell Django what database system to use by editing a configuration file, as we will see in later sections. It is also worth noting that if you want to use MySQL, you will need to install MySQLdb, the MySQL driver for Python.

If you don't have Python 2.5, you can install the python module for SQLite manually by downloading it from http://www.pysqlite.org/ (for Windows users) or through your package manager (for UNIX and Linux users).

Tip

Do I need Apache (or some other web server)?

Django comes with its own web server, and we are going to use it during the development phase, because it is lightweight and comes pre-configured for Django. However, Django does support Apache and other popular web servers such as Lighttpd. We will see how to configure Django for Apache when we prepare our application for deployment later in this book.

The same applies to the database manager. During the development phase, we will use SQLite because it is easy to set up, but when we deploy the application, we will switch to a database server such as MySQL.

As I said earlier, regardless of what components we use, our code will stay the same; Django handles all the communication with the web and database servers for us.

主站蜘蛛池模板: 晋中市| 肥西县| 泗洪县| 伊宁县| 正镶白旗| 沾化县| 北流市| 阜南县| 南漳县| 淮南市| 遵化市| 沙坪坝区| 祥云县| 墨竹工卡县| 博湖县| 陇南市| 永兴县| 舞钢市| 尚义县| 清涧县| 蓝山县| 苏尼特左旗| 石首市| 宜良县| 遵化市| 都兰县| 天门市| 呼伦贝尔市| 苏州市| 沙雅县| 新沂市| 台北县| 临江市| 太原市| 河津市| 无为县| 朝阳市| 公安县| 南陵县| 铜鼓县| 泗阳县|