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
- Visit the official Python website
- Download the latest Python version (3.8+ recommended for Django)
- Run the installer and check "Add Python to PATH" during installation
- Verify the installation by opening Command Prompt and typing:
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:
brew install python
Verify the installation:
python3 --version
Linux
Most Linux distributions come with Python pre-installed. If not, install it using your package manager:
For Ubuntu/Debian:
sudo apt update
sudo apt install python3 python3-pip
For Fedora:
sudo dnf install python3 python3-pip
Verify the installation:
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:
mkdir my_django_project
cd my_django_project
Next, create a virtual environment:
Windows
python -m venv venv
macOS/Linux
python3 -m venv venv
Activating the Virtual Environment
You need to activate the virtual environment before using it:
Windows
venv\Scripts\activate
macOS/Linux
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:
pip install django
Verify the installation:
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:
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:
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:
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:
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:
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:
- Create and activate a virtual environment:
mkdir blog_project
cd blog_project
python -m venv venv
source venv/bin/activate # or venv\Scripts\activate on Windows
- Install Django:
pip install django
- Create a Django project:
django-admin startproject blog .
- Create a blog app:
python manage.py startapp posts
- Register the app in
blog/settings.py
:
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
]
- Run migrations to set up the database:
python manage.py migrate
- Create a superuser to access the admin site:
python manage.py createsuperuser
- Start the development server:
python manage.py runserver
Managing Python Dependencies
It's good practice to track your project's dependencies. Generate a requirements file:
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:
pip install -r requirements.txt
Summary
You've now set up a complete Django development environment! You've learned how to:
- Install Python
- Create and activate a virtual environment
- Install Django
- Create a Django project and application
- Run the development server
- Manage project dependencies
With this foundation, you're ready to start building web applications with Django!
Additional Resources
- Official Django Documentation
- Virtual Environment Documentation
- Django Girls Tutorial
- Django REST Framework for building APIs
Exercises
- Create a new Django project called "portfolio" with an app called "projects"
- Modify the
settings.py
file to include your new app - Create a new virtual environment and install Django with a specific version (e.g., Django 3.2)
- Create a
requirements.txt
file for your project - 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! :)