Posts

Connecting URLs, Views & Templates in Django

  Connecting URLs, Views & Templates in Django Assuming you have a Job model and want to: Show a list of all jobs on the homepage Show details of a single job on a separate page 1. Create Views in jobs/views.py from django.shortcuts import render, get_object_or_404 from .models import Job # List all jobs def homepage(request):     jobs = Job.objects.all()  # Fetch all jobs     return render(request, 'jobs/home.html', {'jobs': jobs}) # Detail view for a single job def job_detail(request, job_id):     job = get_object_or_404(Job, id=job_id)  # Get job or 404 if not found     return render(request, 'jobs/job_detail.html', {'job': job}) 2. Update portfolio/urls.py Add URL patterns for both views: from django.urls import path from jobs import views urlpatterns = [     path('admin/', admin.site.urls),     path('', views.homepage, name='home'),  # H...

How to Use Bootstarp Locally as a Static Asset in Django

Image
  How to Use Bootstrap Locally as a Static Asset in Django In this guide, you’ll learn how to include Bootstrap , jQuery , and Popper.js in your Django project as local static files , instead of using CDN links. 🧩 Step 1: Download Bootstrap, jQuery, and Popper.js 🔹 Bootstrap Visit: https://getbootstrap.com/docs/4.4/getting-started/download Download “ Compiled CSS and JS ” Extract the ZIP file: bootstrap-4.4.1-dist.zip You’ll get: bootstrap.min.css (inside css/ ) bootstrap.min.js (inside js/ ) 🔹 jQuery Visit: https://jquery.com/download/ Right-click “ Compressed, production jQuery 3.4.1 ” and choose: Save link as → jquery-3.4.1.min.js 🔹 Popper.js Visit: https://popper.js.org/ Select Version 1 Download or right-click → Save link as → popper.min.js 📁 Step 2: Move All Files to Django Static Directory Place all downloaded files in: portfolio-project/jobs/static/ Your static folder should now contain: css/bootstrap.min.css js/bootstrap.min.js jquery-3.4.1.min.js popper.min.js ...

How to Add Static Images in Django (with Example)

Image
   How to Add Static Images in Django (with Example) In this tutorial, you’ll learn how to store, serve, and display static images (like .jpg files) in a Django project using proper configuration, URLs, and templates. 🗂️ Step 1: Place Your Image in the Static Folder Inside your Django app directory ( jobs ), create a folder named static , and place your image inside: 📁 jobs/static/mani.jpg ⚙️ Step 2: Configure settings.py Add or confirm the following lines to define your static root directory and URL: import os STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') This tells Django where to collect all static files and how to access them via URL. 🌐 Step 3: Update urls.py to Serve Static Files In your project-level urls.py (e.g., portfolio/urls.py ), import Django’s static helper and update urlpatterns : from django.conf import settings from django.conf.urls.static import static from django.contrib import admin from django.urls import path import j...