Django Guide
Django Guide
bash
python --version
bash
pip --version
• Why: Pip is used to install Django and other packages. Usually, it's installed with
Python.
bash
mkdir DjangoProjects
cd DjangoProjects
bash
• Why: Virtual environments keep your project dependencies isolated from global
Python installation.
bash
env\Scripts\activate
o For PowerShell:
powershell
.\env\Scripts\Activate.ps1
• Where: Terminal
• How:
bash
django-admin --version
• Where: Terminal
• How:
bash
• Why: Creates a new Django project folder with initial configuration files.
bash
code .
bash
Step 12: Create a Django App (Optional but usually next step)
bash
• 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
In PowerShell, run:
powershell
powershell
cd proj1
code .
nginx
Ctrl + `
In VS Code Terminal:
bash
Why: These commands prepare your database by applying the initial Django migrations.
5. Create a New App
Run:
bash
Why: This creates a reusable app component named gallery inside your project.
Open proj1/settings.py
Add the app in INSTALLED_APPS like this:
python
INSTALLED_APPS = [
'gallery.apps.GalleryConfig',
]
cpp
static/
templates/
python
STATICFILES_DIRS = [
BASE_DIR / "static",
]
Why: Tells Django where to look for additional static files (like CSS, JS, images).
'DIRS': [],
Replace it with:
python
Why: Tells Django where your HTML template files are located.
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
• index.html
• login.html
html
Import include:
python
Update urlpatterns:
python
urlpatterns = [
path('', include('gallery.urls')),
]
Why: This tells Django to look for URLs inside the gallery app.
python
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.
python
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.