-
Check if Python is Already Installed: Open a terminal (Linux/Mac) or Command Prompt (Windows) and run:
python --version
or:
python3 --version
If Python is installed, it will display the version number.
-
Download Python: If Python is not installed, download the latest version from the official Python website.
-
Install Python:
- Windows:
- Run the installer and make sure to check "Add Python to PATH" during installation.
- Mac/Linux:
- Use the installer for Mac or package manager (e.g.,
apt
for Ubuntu):sudo apt update sudo apt install python3 python3-pip
- Use the installer for Mac or package manager (e.g.,
- Windows:
-
Verify Installation: Run:
python3 --version
-
Install Virtualenv: Run the following command to install it globally:
pip install virtualenv
-
Verify Installation: Run:
virtualenv --version
-
Create a Project Directory:
mkdir django-tutorial cd django-tutorial
-
Create the Virtual Environment:
virtualenv venv
This creates a virtual environment in the
venv/
directory. -
Activate the Virtual Environment:
- Windows:
venv\Scripts\activate
- Linux/Mac:
source venv/bin/activate
You should see
(venv)
in your terminal prompt, indicating the virtual environment is active. - Windows:
-
Deactivate the Virtual Environment: If needed, deactivate the environment using:
deactivate
-
Activate the Virtual Environment: Make sure your virtual environment is active.
-
Install Django:
pip install django
-
Verify Installation: Run:
django-admin --version
-
Save Dependencies: Save the installed packages to a
requirements.txt
file:pip freeze > requirements.txt
-
Start a Django Project: Run:
django-admin startproject blogproject .
This creates the following structure:
django-tutorial/ manage.py blogproject/ __init__.py asgi.py settings.py urls.py wsgi.py
-
Run the Development Server: Verify the project by running:
python manage.py runserver
Open
http://127.0.0.1:8000/
in your browser. You should see the Django welcome page.
-
What is a Django App? An app is a web application that performs a specific functionality. Multiple apps together make a Django project.
-
Create the Blog App: Run:
python manage.py startapp blog
This creates the following structure:
blog/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py
-
Register the App in
settings.py
: Openblogproject/settings.py
and add'blog',
to theINSTALLED_APPS
list:INSTALLED_APPS = [ ... 'blog', ]
-
Create the Folders: Run:
mkdir templates mkdir static mkdir media
-
Update
settings.py
: Configure these folders inblogproject/settings.py
:TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR / 'templates'], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] STATIC_URL = '/static/' STATICFILES_DIRS = [BASE_DIR / 'static'] MEDIA_URL = '/media/' MEDIA_ROOT = BASE_DIR / 'media'
-
Add a Base Template: Create
templates/base.html
:<!DOCTYPE html> <html lang="en"> <head> <title>{% block title %}Blog Project{% endblock %}</title> <link rel="stylesheet" href="{% static 'css/styles.css' %}"> </head> <body> <header> <h1>Welcome to the Blog Project</h1> <nav> <a href="{% url 'home' %}">Home</a> <a href="{% url 'register' %}">Register</a> </nav> </header> <main> {% block content %}{% endblock %} </main> </body> </html>
-
Create a Static CSS File: Create
static/css/styles.css
:body { font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: #f9f9f9; } header { background: #007BFF; color: white; padding: 10px; text-align: center; } nav a { color: white; margin: 0 10px; text-decoration: none; } main { padding: 20px; }
-
Define the Home View: Add this to
blog/views.py
:from django.shortcuts import render def home(request): return render(request, 'home.html')
-
Map the URL: Add this to
blog/urls.py
:from django.urls import path from . import views urlpatterns = [ path('', views.home, name='home'), ]
-
Update the Project URLs: Include the
blog
app inblogproject/urls.py
:from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('blog.urls')), ]
-
Create the Template: Add
templates/home.html
:{% extends 'base.html' %} {% block title %}Home{% endblock %} {% block content %} <h2>Welcome to the Blog!</h2> <p>This is the homepage.</p> {% endblock %}
Start the development server:
python manage.py runserver
Go to gui directory:
python gui_main.py
Visit:
- Home:
http://127.0.0.1:8000/
- Admin:
http://127.0.0.1:8000/admin/