Previous Page Next Page

Hour 2. Creating Your First Website

What You'll Learn in This Hour

  • How to begin creating a Django project

  • How to start and stop the built-in web server

  • The steps to configure Django to access the database

  • How to create and install an application

  • The steps to apply a model to an application

  • The steps to activate a model in Django

  • How to configure Django to accept specific URL requests

  • How to create a simple view for a web browser

In Hour 1, "Understanding Django," you learned some of the basics about the Django framework. This hour guides you through the steps of creating a functional website called iFriends. Although this website will be basic, it will be the basis for future hours to build on as you are guided through the various aspects of the Django framework.

Creating a Django Project

Let's begin the process of creating a working website by creating a Django project. A Django project is a collection of settings that define a specific instance of Django. These settings include things such as database configuration, URL configuration, and other options that you will learn about as the hours tick by.

Try It Yourself: Create Your First Django Project

Creating a Django project is relatively simple to do from the command prompt. In this section, you create a project called iFriends.

1.
From a command prompt, change to the directory where you want to store the code for the iFriends project.

2.
Create a directory called iFriends. This will be the root directory for the iFriends project.

3.
Change to the iFriends directory.

4.
Type the following command to create the iFriends project:

python django-admin.py startproject iFriends

Watch Out!

Because the project will act as a Python package, avoid using a project name that conflicts with any existing built-in Python packages. The documentation for built-in Python packages can be found at http://www.python.org.


By the Way

There is no need to put your project code in a directory in the web server's document base. The Django framework will be responsible for executing the code. In fact, it is a much better idea to store the code somewhere outside the web server's root. That way your code will be protected from being accessed directly from a web browser.


The startproject command first creates a directory called iFriends, and then it stores the basic set of Python files that are needed to begin the project in the iFriends directory. The startproject command creates the following files:

  • __init__.py is an empty file that tells Python that the website directory should be treated as a Python package.

  • manage.py is the command-line utility that allows the administrator to start and manage the Django project.

  • settings.py is the configuration file that controls the behavior of the Django project.

  • urls.py is a Python file that defines the syntax and configures the behavior of the URLs that will be used to access the website.

The basic purpose of these files is to set up a Python package that Django can use to define the website's structure and behavior. We will discuss these files a bit more in this hour and in subsequent hours as the website gets increasingly complex.


Previous Page Next Page