Preparing the development environment

Arguably the most important part of preparing a new Django project is choosing a name. All kidding aside, the name is important and in that it has a few restrictions: it cannot be the same as a Django or Python module and it should conform to usual Python module naming conventions (no spaces, dashes, or other illegal characters). Django projects have a tradition of naming themselves after jazz musicians (Django itself refers to Django Reinhardt, a jazz guitarist). The project we will build in this book will be named Coleman.

To start with, create a new directory and place it on your PYTHONPATH. The easiest way to do this is to make a directory in the usual way and then create a corresponding .pth file to copy to your system's site-packages. For example, if you're working directory is /home/jesse/ecommerce-projects, then create a single line file called ecommerce.pth that looks like this:

/home/jesse/ecommerce-projects

Copy the file to your system-wide site-packages directory. This varies based on your operating system and how you installed Python. See your documentation or the value of sys.path from the Python interpreter.

Later on in this book we will examine some tools and techniques to drastically simplify Django and Python project dependencies and layouts. One of these tools is called virtualenv, which can help us to better manage $PYTHONPATH. For now, though, we'll do everything the old fashioned way.

Next we will create our project directory and subdirectories. These will correspond to Python modules in our code. From your working directory create two folders: one for our app library and one for our project. Call the first one coleman and the second ecommerce_book. We will refer to the coleman module as our app library and to ecommerce_book as our project module. Next we will create our first app module by creating a directory called products inside our app library module. We will begin building the products app in Chapter 2. Your final directory structure will look like this:

./coleman
./coleman/products
./ecommerce_book

One final piece of preparation: we must create a Python __init__.py file in each of these module locations. This will look like the following:

./coleman/__init__.py
./coleman/products/__init__.py
./ecommerce_book/__init__.py

Lastly, we will create our settings file and a root URLs file in our project module: ecommerce_book/settings.py and ecommerce_book/urls.py. It is recommended that you copy these files from the companion source code. We will then refer to ecommerce_book.settings in our DJANGO_SETTINGS_MODULE.

The project settings file in the companion code assumes that you have a working sqlite3 installation as the database backend (sqlite3 is included with Python 2.5 and later). You can change this by editing the settings.py file, which you will need to do in order to complete the full path to your sqlite3 database file. This is another file that fits nicely into the projects directory, and by placing it there you gain the ability to use Python's os module to locate it. This is more desirable than hard coding a path into settings.py. To take this approach with the database file, for example, you could define a setting to represent the project's location on the file system, and then append the database name:

PROJECT_HOME=os.path.dirname(os.path.realpath(__file__))
DATABASE_NAME=os.path.join(PROJECT_HOME, 'mydatabase.db')

Once you've made the appropriate changes, run django-admin.pysyncdb to create your initial database.

These instructions assume that you already have a working installation of Django and have the django-admin.py script installed on your system's path. For more information on configuring and installing Django, please see the documentation at djangoproject.org/docs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.3.208