0% found this document useful (0 votes)
1 views9 pages

Django Guide

This document provides a step-by-step guide to set up a Django project environment on Windows, including installing Python, creating a virtual environment, and installing Django. It also outlines how to create a new Django project, set up apps, and configure templates and URLs. The guide is divided into three parts, detailing the installation process, project setup, and creating views and templates.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views9 pages

Django Guide

This document provides a step-by-step guide to set up a Django project environment on Windows, including installing Python, creating a virtual environment, and installing Django. It also outlines how to create a new Django project, set up apps, and configure templates and URLs. The guide is divided into three parts, detailing the installation process, project setup, and creating views and templates.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Part-1

Setting Up Django Project Environment on Windows

Step 1: Install Python

• Why: Django is a Python-based framework. Python must be installed first.


• Where: Web browser
• How:
1. Go to: https://www.python.org/downloads/windows/
2. Download the latest Python version for Windows.
3. During installation, check the box: "Add Python to PATH".
4. Click Install Now.

Step 2: Verify Python Installation

• Where: CMD (Command Prompt) or PowerShell


• How:

bash

python --version

• Why: To check if Python is successfully installed.

Step 3: Install pip (Python Package Manager) (Optional if not auto-installed)

• Where: CMD or PowerShell


• How:

bash

pip --version

• Why: Pip is used to install Django and other packages. Usually, it's installed with
Python.

Step 4: Create a Project Folder

• Where: File Explorer or CMD


• How:
Example using CMD:

bash

mkdir DjangoProjects
cd DjangoProjects

• Why: To keep your Django projects organized.

Step 5: Create a Virtual Environment

• Where: CMD, PowerShell, or VS Code Terminal


• How:

bash

python -m venv env

• Why: Virtual environments keep your project dependencies isolated from global
Python installation.

Step 6: Activate the Virtual Environment

• Where: CMD or PowerShell


• How:
o For CMD:

bash

env\Scripts\activate

o For PowerShell:

powershell

.\env\Scripts\Activate.ps1

• Why: To use the environment where Django will be installed.

Step 7: Install Django

• Where: CMD, PowerShell, or VS Code Terminal (after activation)


• How:
bash

pip install django

• Why: Installs the Django framework into the virtual environment.

Step 8: Verify Django Installation

• Where: Terminal
• How:

bash

django-admin --version

• Why: Confirms Django is installed and ready.

Step 9: Start a Django Project

• Where: Terminal
• How:

bash

django-admin startproject proj1


cd proj1

• Why: Creates a new Django project folder with initial configuration files.

Step 10: Open Project in VS Code

• Where: CMD or PowerShell


• How:

bash

code .

• Why: Opens the current directory in Visual Studio Code.

Step 11: Run Django Development Server


• Where: VS Code Terminal (make sure environment is activated)
• How:

bash

python manage.py runserver

• Why: Starts a local server to test your project in the browser.


• Open your browser and go to: http://127.0.0.1:8000/

Step 12: Create a Django App (Optional but usually next step)

• Where: Terminal in project root folder


• How:

bash

python manage.py startapp gallery

• Why: Apps are components inside a Django project for better code organization.
Part-2
Steps to Set Up a Django Project Using PowerShell and VS Code

1. Create a New Django Project

In PowerShell, run:

powershell

django-admin startproject proj1

2. Open the Project in VS Code

Right-click on the proj1 folder and select "Open with Code"


Or use:

powershell

cd proj1
code .

3. Open the VS Code Terminal

Inside VS Code, press:

nginx

Ctrl + `

(This opens the terminal inside VS Code)

4. Run Initial Django Commands

In VS Code Terminal:

bash

python manage.py makemigrations


python manage.py migrate

Why: These commands prepare your database by applying the initial Django migrations.
5. Create a New App

Run:

bash

python manage.py startapp gallery

Why: This creates a reusable app component named gallery inside your project.

6. Register the App in settings.py

Open proj1/settings.py
Add the app in INSTALLED_APPS like this:

python

INSTALLED_APPS = [
'gallery.apps.GalleryConfig',
]

7. Create Static and Templates Folders

In your root project folder (proj1/), create two new folders:

cpp

static/
templates/

8. Set Static Directory in settings.py

In settings.py, add this:

python

STATICFILES_DIRS = [
BASE_DIR / "static",
]

Why: Tells Django where to look for additional static files (like CSS, JS, images).

9. Set Template Directory in settings.py

Find this block in TEMPLATES section:


python

'DIRS': [],

Replace it with:

python

'DIRS': [BASE_DIR / "templates"],

Why: Tells Django where your HTML template files are located.

10. Project is Now Set Up

You can now start building pages and functionality.

11. Create an index.html Page

Inside the templates/ folder, create a file named:

diff

index.html

You can now write your HTML code in it and render it from views.
Part-3
Continue Django Project Setup – Templates, URLs, Views

1. Create HTML Templates

Inside the templates/ folder, create:

• index.html
• login.html

Add basic HTML to test, e.g.:

html

<!-- index.html -->


<h1>Welcome to Home Page</h1>
html

<!-- login.html -->


<h1>Login Page</h1>

2. Edit Project's urls.py

Open proj1/urls.py and do the following:

Import include:

python

from django.urls import path, include

Update urlpatterns:

python

urlpatterns = [
path('', include('gallery.urls')),
]

Why: This tells Django to look for URLs inside the gallery app.

3. Create urls.py in the App (gallery/)

Create a new file named urls.py inside the gallery app.


Paste the following code:

python

from django.urls import path


from gallery import views # replace 'gallery' with your app name if
different

urlpatterns = [
path('', views.index, name='home'),
path('login', views.login, name='login'),
path('logout', views.logout, name='logout'),
]

Why: This defines URL routes handled by the views in your app.

4. Create View Functions in views.py

Open gallery/views.py and define the following functions:

python

from django.shortcuts import render

def index(request):
return render(request, 'index.html')

def login(request):
return render(request, 'login.html')

def logout(request):
return render(request, 'index.html') # or redirect to login/home as
needed

Why: These views handle requests for each URL and render the corresponding template.

You might also like