Django - Environment
Django - Setting Up Your Development Environment
Section titled “Django - Setting Up Your Development Environment”Setting up a proper development environment is the crucial first step to building Django applications. This involves installing Python, setting up a project environment using venv, installing Django itself, and potentially a database system.
Step 1 – Installing Python
Section titled “Step 1 – Installing Python”Django is a Python framework, so you need Python installed. Modern versions of Django require Python 3. Check the official Django documentation for the specific Python 3.x versions supported by the Django version you intend to use (e.g., Django 4.x typically requires Python 3.8+).
Most Linux distributions and macOS versions come with Python 3 pre-installed. Windows users will likely need to install it.
Verifying Installation:
Section titled “Verifying Installation:”Open your terminal or command prompt and run:
python --version# or sometimes python3 --versionIf Python 3 is installed, you’ll see its version number. If not, or if you need a specific version, download it from the official Python website:
- Python Downloads: https://www.python.org/downloads/
Important: During installation on Windows, ensure you check the box that says “Add Python to PATH”.
Step 2 – Setting Up a Virtual Environment (venv)
Section titled “Step 2 – Setting Up a Virtual Environment (venv)”It is highly recommended to use virtual environments for your Python projects. A virtual environment isolates the project’s dependencies (like Django and other libraries) from your global Python installation and other projects.
Creating a Virtual Environment:
Section titled “Creating a Virtual Environment:”Navigate to the directory where you want to create your project, then run:
# Linux/macOSpython3 -m venv venv
# Windowspython -m venv venvThis creates a directory named venv containing a copy of the Python interpreter and a place to install project-specific libraries.
Activating the Virtual Environment:
Section titled “Activating the Virtual Environment:”Before installing packages or running your project, you must activate the environment:
# Linux/macOSsource venv/bin/activate
# Windows (Command Prompt).\venv\Scripts\activate.bat
# Windows (PowerShell).\venv\Scripts\Activate.ps1
# Windows (Git Bash)source venv/Scripts/activateYour command prompt should change to indicate the active environment (e.g., (venv) your-user@machine:...$). To deactivate, simply type deactivate.
Step 3 - Installing Django
Section titled “Step 3 - Installing Django”With your virtual environment activated, install Django using pip, Python’s package installer (which is included in venv):
pip install djangoThis command downloads and installs the latest stable version of Django and its dependencies within your active virtual environment.
To install a specific version:
pip install django==4.2.* # Installs the latest 4.2.x versionVerifying Django Installation:
Section titled “Verifying Django Installation:”Run the following command:
django-admin --versionIf Django is installed correctly within your active environment, this will print its version number.
Step 4 – Database Setup
Section titled “Step 4 – Database Setup”Django requires a database. By default, it’s configured to use SQLite 3, which is stored as a single file in your project directory. SQLite is excellent for development and small applications because it requires no separate server setup.
For larger or production applications, Django officially supports several major database engines:
- PostgreSQL: Often recommended for robust Django deployments.
- MySQL / MariaDB: Very popular open-source databases.
- Oracle: A powerful commercial database.
- SQLite: Built-in, file-based, great for development/testing.
To use databases other than SQLite, you’ll need to:
- Install the database server itself.
- Install the appropriate Python database adapter library within your virtual environment (e.g.,
pip install psycopg2-binaryfor PostgreSQL,pip install mysqlclientfor MySQL). - Configure the
DATABASESsetting in your Django project’ssettings.pyfile with the connection details (engine, name, user, password, host, port).
Support for NoSQL databases (like MongoDB) exists through third-party libraries (e.g., Djongo), but they are not officially part of the core Django framework.
Step 5 – The Development Web Server
Section titled “Step 5 – The Development Web Server”Django includes a lightweight web server specifically for development and testing. It automatically reloads your Python code when changes are detected, making development faster.
You run it from your project’s base directory (where manage.py is located) with:
python manage.py runserverThis starts the server, typically on http://127.0.0.1:8000/.
Remember: This server is only for development. For production deployment, you’ll need a production-grade setup like Nginx or Apache with a WSGI server (Gunicorn, uWSGI, mod_wsgi), as discussed in the deployment chapter.