Overview
This document provides step-by-step instructions for installing Django, setting up a virtual environment, creating a Django project, and running the development server.
Prerequisites
Before installing Django, ensure that the following software is installed on your system:
- Python 3.8 or later
- pip (Python package manager)
Verify Python Installation
Open a terminal or command prompt and run:
python --version
or
python3 --version
Expected output:
Python 3.x.x
Verify pip Installation
pip --version
Expected output:
pip x.x.x from ...
Step 1: Create a Virtual Environment
Using a virtual environment is recommended to isolate project dependencies.
Create the Environment
python -m venv venv
This creates a folder named venv containing the virtual environment.
Activate the Environment
Windows
venv\Scripts\activate
macOS/Linux
source venv/bin/activate
After activation, your terminal prompt should display the environment name.
Example:
(venv) C:\Projects>
Step 2: Install Django
Install Django using pip:
pip install django
Alternatively:
python -m pip install django
Verify Installation
django-admin --version
Example output:
5.2.1
Step 3: Create a Django Project
Create a new Django project:
django-admin startproject myproject
Navigate to the project directory:
cd myproject
Project structure:
myproject/
│
├── manage.py
└── myproject/
├── __init__.py
├── settings.py
├── urls.py
├── asgi.py
└── wsgi.py
Step 4: Run the Development Server
Start the Django development server:
python manage.py runserver
Expected output:
Watching for file changes with StatReloader
Starting development server at http://127.0.0.1:8000/
Open your browser and visit:
http://127.0.0.1:8000/
You should see the Django welcome page.
Step 5: Create a Django Application
Inside the project directory, create an app:
python manage.py startapp myapp
Project structure:
myproject/
│
├── manage.py
├── myapp/
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── views.py
│ └── migrations/
│
└── myproject/
Step 6: Register the Application
Open settings.py and add the application name to INSTALLED_APPS:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
]
Step 7: Apply Database Migrations
Run:
python manage.py migrate
Expected output:
Applying contenttypes...
Applying auth...
Applying admin...
This creates the default database tables.
Common Commands
| Command | Description |
|---|---|
python manage.py runserver | Start development server |
python manage.py startapp appname | Create a new app |
python manage.py makemigrations | Create migration files |
python manage.py migrate | Apply migrations |
python manage.py createsuperuser | Create admin user |
python manage.py shell | Open Django shell |
Troubleshooting
pip Not Found
Install pip:
python -m ensurepip --upgrade
Django Not Recognized
Verify installation:
pip show django
If Django is not installed:
pip install django
Virtual Environment Not Activating
Ensure that the correct activation command is used for your operating system.
Additional Resources
- Official Django documentation: Django Documentation
- Django project website: Django Project Website
Conclusion
You have successfully:
- Installed Python and pip.
- Created a virtual environment.
- Installed Django.
- Created a Django project.
- Started the development server.
- Created and registered a Django application.
- Applied database migrations.
Your Django development environment is now ready for building web applications.