Chapter 2. Creating Your First Website

<feature><title>What You’ll Learn in This Hour</title> <objective>

How to begin creating a Django project

</objective>
<objective>

How to start and stop the built-in web server

</objective>
<objective>

The steps to configure Django to access the database

</objective>
<objective>

How to create and install an application

</objective>
<objective>

The steps to apply a model to an application

</objective>
<objective>

The steps to activate a model in Django

</objective>
<objective>

How to configure Django to accept specific URL requests

</objective>
<objective>

How to create a simple view for a web browser

</objective>
</feature>

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.

Starting the Development Server

After you have created the Django project, you should be able to start the development server to test it. The development server is a lightweight web server that is included with the Django project. It lets you develop and test your website without having to deal with all the configuration and management issues of a production web server.

Configuring the Database

After you have verified that you can start and stop the development server, it is time to configure access to the database. This section takes you through the process of creating and configuring access to the database that will be used in the sample project.

By the Way

Django can dynamically serve web pages without using a database to store information. However, one of the best aspects of Django is its ability to implement database-backed websites.

Configuring the database involves three major steps. The first is to create the database and assign rights. The second is to modify the settings.py file to specify the database type, name, location, and access credentials. The third step is to synchronize the Django project with the database to create the initial tables necessary for the Django engine.

Django supports several different types of database engines. The project used in this book uses a MySQL database. This section assumes that you have already installed, configured, and started a database engine and that it is accessible from the development server.

Watch Out!

The MySQL database does not allow you to use case sensitive names when creating tables. If you want to define objects in your project that have uppercase characters, then you will need to turn off case sensitivity in the Django framework by using the following setting in the <django installation path>/django/db/backends/__init__.py file:

uses_case_insensitive_names = True

Configuring Database Access in settings.py

After the database has been created and a user account set up for Django, you need to configure the settings.py file in your Django project to access that database. Each Django project has its own settings.py file. The settings.py file is a Python script that configures various project settings.

Django uses the following settings in the settings.py file to control access to the database:

  • DATABASE_ENGINE is the type of database engine. Django accepts postgresql_psycopg2, postgresql, mysql, mysql_old, sqlite3, and ado_mssql.

  • DATABASE_NAME is the name of the database. For SQLite, you need to specify the full path.

  • DATABASE_USER is the user account to use when connecting to the database. No user is used with SQLite.

  • DATABASE_PASSWORD is the password for DATABASE_USER. No password is used with SQLite.

  • DATABASE_HOST is the host on which the database is stored. This can be left empty for localhost. No host is specified with SQLite.

  • DATABASE_PORT is the port to use when connecting to the database. This can be left empty for the default port. No port is specified with SQLite.

Synchronizing the Project to the Database

After you have configured access to the database in the settings.py file, you can synchronize your project to the database. Django’s synchronization process creates the tables necessary in the database to support your project.

The tables are created based on what applications are specified in the INSTALLED_APPS setting of the settings.py file. The following are the default settings already specified in the INSTALLED_APPS setting:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
)

The following list describes the default applications that get installed in the Django project:

  • django.contrib.auth is the default authentication system included with Django.

  • django.contrib.contenttypes is the framework of types of content.

  • django.contrib.sessions is the framework used to manage sessions.

  • django.contrib.sites is the framework used to manage multiple sites using a single Django installation.

Installing an Application

After you have configured and synchronized the database, you can begin installing applications in it. Installing applications is simply a matter of creating an application directory, defining a model, and then activating the application so that Django can access it in the database.

Creating a Model

After the application has been created, you need to create a model for the data that will be stored in the application. A model is simply a definition of the classes, attributes, and relationships of objects in the application.

To create a model, you need to modify the models.py file located in the application directory. The models.py file is a Python script that is used to define the tables that will be added to the database to store objects in the model.

The models.py file initially has only one line, which imports the models object from the django.db package. To define the model, you need to define one or more classes. Each class represents an object type in the database.???

Adding Data Using the API

This section briefly describes how to use the Django shell interface and database API to quickly add a single Person object to the People table. The Django shell is a Python shell that gives you access to the database API included with Django. The database API is a set of Python methods that allow you to access the project database from the data model.

Setting Up the URLConf File

This section discusses configuring the URLConf file to define how installed applications are accessed from the web. The URLConf file is a Python script that allows you to define specific views that are accessed based on the URL that is sent by the web browser. When the Django server receives an URL request, it parses the request based on the patterns that are contained in the URLConf file. The parsed request is translated into a specific Python function that is executed in the views.py file, discussed in a moment.

By the Way

The location of the URLConf file is defined by the ROOT_URLCONF setting in the settings.py file. The default location is the name of the project’s root directory. In the case of the iFriends project, the value of ROOT_URLCONF would be set to the following value, where 'iFriends.urls' equates to iFriends/urls.py:

ROOT_URLCONF = 'iFriends.urls'

Creating a Simple View

After you have configured the URLConf file, you need to add the views to the application. The application’s views are stored as functions in the views.py file in the application directory. When the Django server receives an URL request, it parses the request based on the patterns that are contained in the URLConf file and determines which function to execute to generate the web view.

Summary

In this hour, you created a Django project called iFriends. You configured access to a MySQL database for the project. You created an application called People, added a Person class, and populated the database with one Person object. You then configured the URL behavior to support an index view and added the necessary code in that view to display a list of objects in the Person class.

The steps you took during this hour helped demonstrate how easy it is to set up a website using the Django framework. Subsequent hours will build on this framework to implement a full-featured website.

Q&A

Q.

How do I modify a model after it has been synced to the database?

A.

Currently, Django cannot update models reliably. The safest and easiest method to modify an existing model is to make changes to the model and then delete all tables related to the model in the database using the SQL drop command. Finally, use the syncdb command to sync the model with the database.

Q.

Is there a way to check for errors in my model before trying to sync to the database?

A.

Django has a utility to validate the contents of models before syncing to the database. From the root directory of the project, enter python manage.py validate. The validate utility checks the model’s syntax and logic and reports any problems.

Workshop

The workshop consists of a set of questions and answers designed to solidify your understanding of the material covered in this hour. Try answering the questions before looking at the answers.

Quiz

1.

What file contains the information that Django uses to connect to the database?

2.

What default file contains the configuration that Django uses to parse the location URLs?

3.

What file contains the code that implements an index view for the People application in the iFriends project?

Quiz Answers

1.

settings.py

2.

urls.py

3.

iFriends/People/views.py

Exercises

Try your hand at creating and activating a simple application. Create an application called Comments. Add a class to the model called Note, with two CharField attributes called Title and Text. Then activate the model by adding it to the INSTALLED_APPS setting in the settings.py file. Synchronize the model to the database. Test your application by adding an object to the database using the Django shell.

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

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