0
0
Djangoframework~20 mins

Static files in templates in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Static Files Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django template snippet?
Given the following Django template code, what will be the rendered HTML output assuming STATIC_URL = '/static/' and the static file css/style.css exists?

{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
Django
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
A<link rel="stylesheet" href="/static/css/style.css">
B<link rel="stylesheet" href="static/css/style.css">
C<link rel="stylesheet" href="/css/style.css">
D<link rel="stylesheet" href="/assets/css/style.css">
Attempts:
2 left
💡 Hint
Remember that {% static %} prepends the STATIC_URL setting to the file path.
📝 Syntax
intermediate
2:00remaining
Which option correctly loads and uses a static image in a Django template?
Select the Django template code snippet that correctly loads the static tag and displays an image located at images/logo.png.
A
{% load staticfiles %}
&lt;img src="static 'images/logo.png'" alt="Logo"&gt;
B<img src="{% static 'images/logo.png' %}" alt="Logo">
C
{% load static %}
&lt;img src="static('images/logo.png')" alt="Logo"&gt;
D
{% load static %}
&lt;img src="{% static 'images/logo.png' %}" alt="Logo"&gt;
Attempts:
2 left
💡 Hint
You must load the static template tag library before using {% static %}.
🔧 Debug
advanced
2:00remaining
Why does this Django template raise a TemplateSyntaxError?
Consider this Django template snippet:

{% load static %}
<img src="{% static images/logo.png %}" alt="Logo">

Why does it raise a TemplateSyntaxError?
Django
{% load static %}
<img src="{% static images/logo.png %}" alt="Logo">
ABecause the static tag requires parentheses around the path.
BBecause the static tag cannot be used inside an img tag.
CBecause the static tag argument must be a quoted string, but here it is unquoted.
DBecause the static tag must be loaded after the img tag.
Attempts:
2 left
💡 Hint
Check how the path argument is passed to the static tag.
state_output
advanced
2:00remaining
What is the value of the rendered src attribute in this Django template?
Given STATIC_URL = '/assets/' in settings.py, what will be the value of the src attribute after rendering this template?

{% load static %}
<img src="{% static 'img/photo.jpg' %}" alt="Photo">
Django
{% load static %}
<img src="{% static 'img/photo.jpg' %}" alt="Photo">
Aimg/photo.jpg
B/assets/img/photo.jpg
C/static/img/photo.jpg
D/media/img/photo.jpg
Attempts:
2 left
💡 Hint
The static tag uses the STATIC_URL setting as prefix.
🧠 Conceptual
expert
2:00remaining
Which statement about Django static files in templates is TRUE?
Choose the correct statement about using static files in Django templates.
AThe {% static %} tag must be loaded with {% load static %} before use, and it generates URLs based on STATIC_URL setting.
BStatic files can be referenced directly without loading any tag, and Django automatically resolves their URLs.
CThe {% static %} tag requires the full absolute URL including domain to work correctly.
DStatic files must be placed inside the templates directory to be accessible via {% static %}.
Attempts:
2 left
💡 Hint
Think about how Django manages static files and the role of the static template tag.