Django

Display Image from Static Folder using {% static %}

๐Ÿ“Œ Introduction

In Django, images are also static files.
We store them inside the static/ folder and load them using:

๐Ÿ‘‰ {% static %}


๐ŸŽฏ Program Statement

๐Ÿ‘‰ Display an image from the static folder using {% static %}.


๐Ÿง  Concept

๐Ÿ‘‰ Static folder can store:

  • CSS
  • JS
  • Images

โš™๏ธ Step 1: Static Folder Structure


๐Ÿ“ Project Structure:

myproject/
static/
images/
pic.jpg

๐Ÿ“ File: pic.jpg

๐Ÿ”น Path:

myproject/static/images/pic.jpg

๐Ÿ‘‰ You can use any image (e.g., logo, photo, etc.)


โš™๏ธ Step 2: Ensure Static Settings


๐Ÿ“ File: settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]

โš™๏ธ Step 3: Create Template


๐Ÿ“ File: image.html

๐Ÿ”น Path:

myproject/templates/image.html

๐Ÿ”น Code:

{% load static %}

<!DOCTYPE html>
<html>
<head>
<title>Image Display</title>
</head>
<body>

<h1>Displaying Image</h1>

<img src="{% static 'images/pic.jpg' %}" width="300" height="200">

</body>
</html>

โš™๏ธ Step 4: Create View


๐Ÿ“ File: views.py

๐Ÿ”น Path:

myproject/myapp/views.py

๐Ÿ”น Code:

from django.shortcuts import render

def show_image(request):
return render(request, 'image.html')

โš™๏ธ Step 5: URL Mapping


๐Ÿ“ File: urls.py

๐Ÿ”น Path:

myproject/myproject/urls.py

๐Ÿ”น Code:

from django.contrib import admin
from django.urls import path
from myapp import views

urlpatterns = [
path('admin/', admin.site.urls),
path('image/', views.show_image),
]

โš™๏ธ Step 6: Run Server

python manage.py runserver

๐ŸŒ Step 7: Output

๐Ÿ‘‰ http://127.0.0.1:8000/image/


โœ… Output:

You will see the image displayed on the page.


๐Ÿง  How It Works

  1. Image stored in static/images/
  2. {% load static %} enables static usage
  3. {% static 'images/pic.jpg' %} generates path
  4. <img> tag displays image

๐Ÿ”ฅ Key Syntax


Load Static

{% load static %}

Display Image

<img src="{% static 'images/pic.jpg' %}">

โš ๏ธ Common Errors


โŒ Forgot {% load static %}

๐Ÿ‘‰ Image will not load


โŒ Wrong path

โŒ Wrong:

{% static 'pic.jpg' %}

โœ… Correct:

{% static 'images/pic.jpg' %}

โŒ Image not in static folder

๐Ÿ‘‰ Must be inside:

static/images/

โŒ Browser cache issue

๐Ÿ‘‰ Do hard refresh:

Ctrl + Shift + R

๐Ÿงช Practice Questions

  1. Display multiple images
  2. Add image in header
  3. Create gallery page
  4. Add CSS styling to image

๐ŸŽค Viva Questions & Answers


1. What is static file in Django?

Static files include CSS, JS, and images used in UI.


2. Where are images stored?

Inside static/ folder, usually in images/ subfolder.


3. How to display image in Django template?

Using <img> tag with {% static %}.


4. Why use {% load static %}?

To enable static file handling in templates.


5. What happens if image path is wrong?

Image will not be displayed.


6. What is <img> tag?

HTML tag used to display images.


7. Can we style images using CSS?

Yes, CSS can control size, border, alignment.


8. What is relative path in static?

Path relative to static folder.


9. Can we use multiple images?

Yes, multiple images can be loaded.


10. Why is this important?

Images are essential for UI design and user experience.


๐Ÿ‘‰ Next Post: Add JavaScript via Static Folder
๐Ÿ‘‰ Back to List: Django Programs (60 Questions with Solutions)


Further Reading

Introduction to Django Framework and its Features

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *