Django Installation
Introduction
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. Before you can start building amazing web applications with Django, you need to properly install it and set up your development environment.
In this guide, you'll learn:
- What Django is and why it's useful
- Prerequisites for installing Django
- How to set up a virtual environment
- Installing Django using pip
- Verifying your installation
- Creating your first Django project
Let's get started on your Django journey!
Prerequisites
Before installing Django, make sure you have:
- Python - Django requires Python. As of this writing, Django 4.2 supports Python 3.8 and higher. You can check your Python version with:
python --version
If you don't have Python installed, download and install it from python.org.
- pip - The Python package installer. It usually comes with Python, but you can verify its installation with:
pip --version
If it's not installed, follow the pip installation guide.
Setting Up a Virtual Environment
When working with Python projects, it's considered best practice to use virtual environments. A virtual environment is an isolated space where you can install Python packages without affecting other projects or your system's global Python installation.
Why Use Virtual Environments?
- Isolates project dependencies
- Prevents version conflicts between projects
- Makes your project portable and reproducible
- Simplifies dependency management
Creating a Virtual Environment
For Windows:
# Create a project directory
mkdir django_project
cd django_project
# Create a virtual environment
python -m venv venv
# Activate the virtual environment
venv\Scripts\activate
For macOS/Linux:
# Create a project directory
mkdir django_project
cd django_project
# Create a virtual environment
python -m venv venv
# Activate the virtual environment
source venv/bin/activate
When activated, your command prompt should change to indicate that you're now working within the virtual environment. It typically shows the name of the virtual environment in parentheses at the beginning of the prompt.
Installing Django
Once your virtual environment is activated, you can install Django using pip:
pip install django
This command will download and install the latest stable version of Django.
If you need a specific version of Django, you can specify it:
pip install django==4.2.0
Verifying the Installation
To verify that Django has been installed correctly, run the following command:
python -c "import django; print(django.get_version())"
This should display the Django version that you installed.
Creating Your First Django Project
Now that Django is installed, you can create your first project:
django-admin startproject mysite
This command creates a Django project named "mysite" with the following structure:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
Let's understand what each file does:
- manage.py: A command-line utility that lets you interact with your Django project.
- mysite/init.py: An empty file that tells Python that this directory should be considered a Python package.
- mysite/settings.py: Settings/configuration for your Django project.
- mysite/urls.py: URL declarations for your Django project; your "table of contents".
- mysite/asgi.py: An entry-point for ASGI-compatible web servers to serve your project.
- mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.
Running the Development Server
Navigate to your project directory and start the development server:
cd mysite
python manage.py runserver
You should see output similar to this:
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.
March 14, 2023 - 15:50:53
Django version 4.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Open your web browser and visit http://127.0.0.1:8000/
. You should see a rocket taking off with a welcome message, confirming that your Django installation was successful!
Common Installation Issues and Solutions
Issue 1: "Command not found" errors
If you encounter "command not found" errors when trying to run django-admin
, make sure:
- Your virtual environment is activated
- Django is installed in your active environment
Issue 2: Database errors
If you see database-related errors, you might need to install database drivers. For example, for PostgreSQL:
pip install psycopg2-binary
For MySQL:
pip install mysqlclient
Issue 3: Permission issues (especially on Unix-based systems)
If you encounter permission errors, try using sudo
or checking your file system permissions:
sudo pip install django
However, using sudo
with pip is generally not recommended. Instead, use virtual environments or user installs with pip install --user django
.
Real-World Project Setup Example
Let's create a more practical example of setting up a Django project for a blog site:
# Create and activate virtual environment (steps from earlier)
mkdir blog_project
cd blog_project
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install Django and required packages
pip install django
pip install pillow # For image processing
pip install markdown # For processing markdown content
# Create a requirements.txt file for future reference
pip freeze > requirements.txt
# Create the Django project
django-admin startproject blogsite
# Navigate to the project
cd blogsite
# Create a blog app within the project
python manage.py startapp blog
# Run initial migrations
python manage.py migrate
# Create a superuser for the admin interface
python manage.py createsuperuser
# Run the development server
python manage.py runserver
This example demonstrates a more complete workflow, including creating an app within your project and setting up additional packages that might be needed for a blog.
Summary
In this guide, you've learned how to:
- Set up a Python virtual environment
- Install Django using pip
- Verify your Django installation
- Create your first Django project
- Run the Django development server
- Handle common installation issues
- Set up a more practical Django project structure
With Django successfully installed, you're now ready to start building powerful web applications. The next steps would be to learn about Django's MTV (Model-Template-View) architecture, create your first app, and understand how to work with databases in Django.
Additional Resources
- Official Django Documentation
- Django Girls Tutorial
- Django for Beginners by William S. Vincent
- Django Web Framework (Mozilla Developer Network)
Exercises
- Create a new Django project named "portfolio" and start the development server.
- Modify the settings.py file to use a different database (like PostgreSQL or MySQL).
- Create a new app within your project called "projects" that will showcase your coding projects.
- Try installing Django in a different virtual environment with a specific older version (e.g., Django 3.2).
- Create a requirements.txt file for your project and try setting up the same environment on a different computer or directory.
Now you're ready to dive deeper into Django development!
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)