Creating a New Django Project

Tutorial 3 of 5

1. Introduction

In this tutorial, we will learn how to create a new Django project from scratch. Django is a high-level Python web framework that promotes rapid development and clean, pragmatic design.

We will cover how to:

  • Use Django's command-line tools to create a new project
  • Understand the structure of a Django project
  • Explore the configuration files generated by Django

Prerequisites:

  • A basic understanding of Python programming
  • Django installed on your system. If it's not installed, you can do so by running pip install Django in your terminal.

2. Step-by-Step Guide

2.1. Creating a New Django Project

A Django project is a collection of settings and configurations for an instance of Django, including database configuration, Django-specific options, and application-specific settings.

To create a new Django project, navigate to the directory where you want your project to live, and run the following command:

django-admin startproject my_project

Replace my_project with your preferred project name. Django will create a new directory with the same name as your project, which will contain the basic files and directories necessary for a Django project.

2.2. Understanding the Django Project Structure

After running the above command, your project directory should look like this:

my_project/
    manage.py
    my_project/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

Here's what each file does:

  • manage.py: A command-line utility that lets you interact with your Django project.
  • my_project/: The inner my_project/ directory is the actual Python package for your project.
  • __init__.py: An empty file that tells Python that this directory should be considered a Python package.
  • settings.py: Settings/configuration for this Django project.
  • urls.py: The URL declarations for this Django project.
  • asgi.py: An entry-point for ASGI-compatible web servers.
  • wsgi.py: An entry-point for WSGI-compatible web servers.

3. Code Examples

3.1. Running the Django Development Server

To verify that your project has been set up correctly, navigate to your project directory (my_project/) and run the following command:

python manage.py runserver

This command starts the Django development server, which is a lightweight web server written purely in Python. After running the command, you should see output similar to the following:

Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

You can now open your web browser and visit http://127.0.0.1:8000/ to view your new Django project.

4. Summary

In this tutorial, we learned how to create a new Django project, explored the structure and purpose of various files in a Django project, and ran our project using Django's development server.

For further learning, you can explore how to create and manage Django applications, how to connect Django to a database, and how to use Django's models and views to design your website's backend.

5. Practice Exercises

  1. Create a new Django project in a different directory and compare its structure with the one we created in this tutorial.
  2. Modify the settings.py file in your new Django project to change the timezone to your local timezone. (Hint: Look for the TIME_ZONE setting.)
  3. Use the Django development server to run your new Django project and visit it in your web browser.

Solutions:

  1. The structure of the new Django project should be identical to the structure of the project we created in this tutorial.
  2. Open settings.py and find the TIME_ZONE setting. Change its value to your local timezone, for example, 'America/New_York'.
  3. Navigate to your project directory and run python manage.py runserver. Open your web browser and visit http://127.0.0.1:8000/.

Keep practicing and exploring Django to become more comfortable with it. Happy coding!