Step 1: Install Python and Pip
Update the package list:
sudo apt update
Install Python 3 and pip:
sudo apt install python3 python3-pip -y
Verify the installation:
python3 --version
pip3 --version
Step 2: Install Virtual Environment
Install venv
(if not installed):
sudo apt install python3-venv -y
Create a virtual environment:
Navigate to your project directory and create the environment:
mkdir my_django_project
cd my_django_project
python3 -m venv env
Activate the virtual environment:
source env/bin/activate
Your terminal prompt should now show (env)
.
Step 3: Install Django
Install Django:
pip install django
Verify the installation:
django-admin --version
Step 4: Start a Django Project
Create a new project:
django-admin startproject mysite
Navigate to the project directory:
cd mysite
To verify that everything is working, run the Django development server.
python3 manage.py runserver
Step 7: Create a View
Edit myapp/views.py
:
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
Create a URL pattern:
Edit mysite/urls.py
to include your app’s view:
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
]
Run the server again:
python3 manage.py runserver
Step 8: Create a Template
mkdir -p myapp/templates
Create an HTML file myapp/templates/home.html
:
<!DOCTYPE html>
<html>
<head>
<title>Django App</title>
</head>
<body>
<h1>Welcome to Django!</h1>
</body>
</html>
Modify the view to render the template:
In myapp/views.py
:
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
Run the server and refresh the browser. You should now see the HTML content rendered.