📖 Scenario: You are building a simple Django web page that needs to show a logo image and apply some styling using CSS.Static files like images and CSS must be properly linked in your HTML templates to display correctly.
🎯 Goal: Create a Django template that correctly loads static files and uses them to display a logo image and apply a CSS style.
📋 What You'll Learn
Create a Django template file named index.html.
Load the static template tag library at the top of the template.
Use the {% static %} tag to link to a CSS file named styles.css located in the static folder.
Use the {% static %} tag to display an image named logo.png located in the static folder.
Add a link tag to include the CSS file in the template's <head> section.
Add an <img> tag to show the logo image in the <body>.
💡 Why This Matters
🌍 Real World
Websites often need to include images, stylesheets, and scripts. Django's static files system helps organize and serve these files efficiently.
💼 Career
Knowing how to properly include and manage static files in Django templates is essential for web developers working with Django to build professional websites.
Progress0 / 4 steps
1
Create the basic HTML template
Create a Django template file named index.html with a basic HTML5 structure including <html>, <head>, and <body> tags.
Django
Hint
Start with a simple HTML5 page structure with <html>, <head>, and <body> tags.
2
Load static template tag and link CSS file
At the very top of index.html, add the Django template tag to load static files using {% load static %}. Then inside the <head> section, add a <link> tag to include the CSS file located at static/styles.css using the {% static 'styles.css' %} tag.
Django
Hint
Use {% load static %} at the top to enable static file usage. Then add a <link> tag with href="{% static 'styles.css' %}" inside the <head>.
3
Add the logo image using static tag
Inside the <body> tag, add an <img> tag to display the image logo.png from the static folder. Use the {% static 'logo.png' %} tag for the src attribute. Add an alt attribute with the text Company Logo for accessibility.
Django
Hint
Use <img src="{% static 'logo.png' %}" alt="Company Logo"> inside the <body> to show the image.
4
Add a CSS class to the image for styling
Add a CSS class named logo to the <img> tag you created. This will allow the CSS file to style the logo image.
Django
Hint
Add class="logo" inside the <img> tag to connect it with CSS styling.
Practice
(1/5)
1. What is the purpose of using {% load static %} at the beginning of a Django template?
easy
A. To start a for loop in the template
B. To load dynamic content from the database
C. To include another template inside the current one
D. To enable the use of static files like images, CSS, and JavaScript in the template
Solution
Step 1: Understand the role of static files in Django templates
Static files are images, CSS, and JavaScript that enhance the website's appearance and behavior.
Step 2: Recognize the function of {% load static %}
This tag loads the static template tag library, enabling the use of {% static %} to reference static files correctly.
Final Answer:
To enable the use of static files like images, CSS, and JavaScript in the template -> Option D
Quick Check:
Load static = enable static files [OK]
Hint: Remember: load static must be at the top to use static files [OK]
Common Mistakes:
Forgetting to add {% load static %} before using {% static %}
Confusing static files with dynamic content
Using {% load static %} in views instead of templates
2. Which of the following is the correct syntax to include a CSS file named styles.css located in the static folder inside a Django template?
easy
A.
B.
C.
D.
Solution
Step 1: Recall the correct way to reference static files in Django templates
Use the {% static 'path/to/file' %} tag after loading static files with {% load static %}.
Step 2: Identify the correct syntax for linking CSS
The correct syntax uses {% static 'styles.css' %} inside the href attribute.
Final Answer:
<link href="{% static 'styles.css' %}" rel="stylesheet"> -> Option A
Quick Check:
Use {% static 'file' %} for static URLs [OK]
Hint: Use {% static 'file' %} inside href for static files [OK]
Common Mistakes:
Using raw paths without {% static %} tag
Using {{ static() }} which is invalid
Trying to load static inside the href attribute
3. Given the template code below, what will be the rendered HTML output if the static file URL for images/logo.png is /static/images/logo.png?
{% load static %}
medium
A.
B.
C.
D.
Solution
Step 1: Understand the role of {% static %} tag
The {% static %} tag converts the file path into the correct URL for static files, here it becomes /static/images/logo.png.
Step 2: Determine the final rendered HTML
The template renders the img tag with src set to the static URL, so the output is <img src="/static/images/logo.png" alt="Logo">.
Final Answer:
<img src="/static/images/logo.png" alt="Logo"> -> Option B
Quick Check:
Static tag outputs correct URL [OK]
Hint: Static tag outputs full static URL in rendered HTML [OK]
Common Mistakes:
Expecting the template tag to appear in output
Confusing static URL with media URL
Omitting {% load static %} causing errors
4. You wrote this Django template code to include a JavaScript file but it does not load correctly:
{% load static %}
What is the error and how can you fix it?
medium
A. Missing {% load static %} tag; add it at the top
B. Wrong file path; fix by changing src to /static/js/app.js
C. Missing {% static %} tag; fix by changing src to {% static 'js/app.js' %}
D. JavaScript files cannot be loaded this way in Django
Solution
Step 1: Identify the incorrect static file reference
The src attribute uses a raw path "static/js/app.js" without the {% static %} tag, so Django won't resolve it correctly.
Step 2: Correct the syntax to use {% static %} tag
Use src="{% static 'js/app.js' %}" to let Django generate the correct URL.
Final Answer:
Missing {% static %} tag; fix by changing src to {% static 'js/app.js' %} -> Option C
5. You want to include an image and a CSS file in your Django template. The image is at images/photo.jpg and the CSS file is at css/style.css inside your static folder. Which template code correctly includes both files?
hard
A. {% load static %}
B. {% load static %}
C.
D. {% load static %}
Solution
Step 1: Ensure {% load static %} is included at the top
This is required to use the {% static %} tag in the template.
Step 2: Use {% static %} tag to reference both image and CSS files
Both src and href attributes must use {% static 'path' %} to generate correct URLs.