This guide provides step-by-step instructions for setting up Python, pip, virtual environments, and Django on a Linux system.
Ensure Python 3 is installed on your system. You can check this by running:
python3 --version
pip3 is the package manager for Python, allowing you to easily install and manage Python libraries.
sudo apt update
sudo apt install python3-pip
pip3 --version # Verify the installation
The python3-venv module is used to create isolated Python environments.
sudo apt update
sudo apt install python3-venv
- Create the virtual environment:
python3 -m venv myenv && source myenv/bin/activate
pip3 install django && django-admin startproject myproject . && python manage.py startapp myapp && python manage.py migrate && python manage.py createsuperuser --username ad --email admin@example.com && pip3 freeze > requirements.txt && python manage.py runserver
Once inside the virtual environment, install Django:
pip3 install django djangorestframework
To exit the virtual environment, simply run:
deactivate
- Create a New Django Project:
django-admin startproject myproject
- Create a New Django App:
python manage.py startapp myapp
- Make Migrations: This command creates migration files for any changes made to the models.
python manage.py makemigrations
- Apply Migrations: This command applies the migration files to the database.
python manage.py migrate
- Create a Superuser: Set up an admin account to access Django’s admin interface.
python manage.py createsuperuser --username ad --email admin@example.com
- Run the Development Server: Start the Django development server to view your project in the browser.
python manage.py runserver