Skip to main content

Django Environment Setup

Before you can start building web applications with Django, you need to set up your development environment. This guide will walk you through the process step-by-step, from installing Python to creating your first Django project.

Introduction

Django is a powerful Python web framework that enables rapid development of secure and maintainable websites. Setting up the right environment is crucial for a smooth development experience. In this tutorial, we'll configure everything you need to start building Django applications.

Prerequisites

Before setting up Django, you'll need:

  • Basic knowledge of command-line operations
  • A text editor or IDE (like VS Code, PyCharm, or Sublime Text)
  • Internet connection to download required packages

Step 1: Install Python

Django is a Python framework, so the first step is to install Python on your system.

Windows

  1. Visit the official Python website
  2. Download the latest Python version (3.8+ recommended for Django)
  3. Run the installer and check "Add Python to PATH" during installation
  4. Verify the installation by opening Command Prompt and typing:
bash
python --version

You should see output like:

Python 3.10.4

macOS

macOS comes with Python pre-installed, but it's usually an older version. Install a newer version using Homebrew:

bash
brew install python

Verify the installation:

bash
python3 --version

Linux

Most Linux distributions come with Python pre-installed. If not, install it using your package manager:

For Ubuntu/Debian:

bash
sudo apt update
sudo apt install python3 python3-pip

For Fedora:

bash
sudo dnf install python3 python3-pip

Verify the installation:

bash
python3 --version

Step 2: Set Up a Virtual Environment

Virtual environments allow you to create isolated spaces for your Python projects, preventing package conflicts between different projects.

Creating a Virtual Environment

First, create a directory for your Django project:

bash
mkdir my_django_project
cd my_django_project

Next, create a virtual environment:

Windows

bash
python -m venv venv

macOS/Linux

bash
python3 -m venv venv

Activating the Virtual Environment

You need to activate the virtual environment before using it:

Windows

bash
venv\Scripts\activate

macOS/Linux

bash
source venv/bin/activate

When activated, you'll see the virtual environment name in your command prompt:

(venv) C:\Users\username\my_django_project>

Step 3: Install Django

With your virtual environment activated, install Django using pip:

bash
pip install django

Verify the installation:

bash
python -m django --version

You should see the Django version number (e.g., 4.2.0).

Step 4: Create Your First Django Project

Now that Django is installed, let's create a new project:

bash
django-admin startproject mysite .

The period (.) at the end creates the project in the current directory instead of creating a new directory.

Your project structure should look like this:

my_django_project/
├── manage.py
├── mysite/
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── venv/

Step 5: Run the Development Server

Let's make sure everything works by starting the development server:

bash
python manage.py runserver

You should see output similar to:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
July 04, 2023 - 12:34:56
Django version 4.2.0, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Open your browser and navigate to http://127.0.0.1:8000/. You should see Django's default welcome page:

Django Welcome Page

Step 6: Configure Your Database

Django uses SQLite by default, which is perfect for development. No additional configuration is required for now.

If you want to set up another database like PostgreSQL or MySQL, you'll need to modify the DATABASES configuration in settings.py.

Step 7: Create a Django Application

A Django project can contain multiple apps. Let's create one:

bash
python manage.py startapp polls

This creates a new directory with the following structure:

polls/
├── __init__.py
├── admin.py
├── apps.py
├── migrations/
│ └── __init__.py
├── models.py
├── tests.py
└── views.py

Step 8: Install Essential Tools for Development

Let's install a few additional packages that are commonly used in Django development:

bash
pip install djangorestframework  # For building APIs
pip install django-debug-toolbar # For debugging
pip install django-crispy-forms # For better form rendering

Real-World Example: Setting Up a Blog Project

Let's put everything together by setting up a simple blog project:

  1. Create and activate a virtual environment:
bash
mkdir blog_project
cd blog_project
python -m venv venv
source venv/bin/activate # or venv\Scripts\activate on Windows
  1. Install Django:
bash
pip install django
  1. Create a Django project:
bash
django-admin startproject blog .
  1. Create a blog app:
bash
python manage.py startapp posts
  1. Register the app in blog/settings.py:
python
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'posts', # Add your new app here
]
  1. Run migrations to set up the database:
bash
python manage.py migrate
  1. Create a superuser to access the admin site:
bash
python manage.py createsuperuser
  1. Start the development server:
bash
python manage.py runserver

Managing Python Dependencies

It's good practice to track your project's dependencies. Generate a requirements file:

bash
pip freeze > requirements.txt

This creates a file listing all installed packages and their versions.

When setting up the project on another machine, you can install all dependencies with:

bash
pip install -r requirements.txt

Summary

You've now set up a complete Django development environment! You've learned how to:

  1. Install Python
  2. Create and activate a virtual environment
  3. Install Django
  4. Create a Django project and application
  5. Run the development server
  6. Manage project dependencies

With this foundation, you're ready to start building web applications with Django!

Additional Resources

Exercises

  1. Create a new Django project called "portfolio" with an app called "projects"
  2. Modify the settings.py file to include your new app
  3. Create a new virtual environment and install Django with a specific version (e.g., Django 3.2)
  4. Create a requirements.txt file for your project
  5. Research how to use different databases with Django (PostgreSQL, MySQL)


If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)