Challenge - 5 Problems
Messages Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Django template snippet?
Given the following Django template code that uses the messages framework, what will be rendered if there is one message with level 'info' and text 'Welcome back!'?
Django
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message.level_tag }}: {{ message }}</li>
{% endfor %}
</ul>
{% else %}
<p>No messages</p>
{% endif %}Attempts:
2 left
💡 Hint
Remember that message.level_tag is lowercase by default.
✗ Incorrect
The messages framework provides message.level_tag in lowercase, so 'info' is rendered exactly as 'info'. The message text is 'Welcome back!'.
📝 Syntax
intermediate2:00remaining
Which option correctly adds a success message in a Django view?
You want to add a success flash message in a Django view function. Which code snippet correctly does this?
Django
from django.contrib import messages def my_view(request): # Add success message here pass
Attempts:
2 left
💡 Hint
Check the order of arguments in the messages.success function.
✗ Incorrect
The correct syntax is messages.success(request, message_text). The request object must be the first argument.
🔧 Debug
advanced3:00remaining
Why does this Django view not display any flash messages?
Consider this Django view code:
from django.contrib import messages
def my_view(request):
messages.error(request, 'An error occurred')
return render(request, 'my_template.html')
The template uses the standard messages loop. Why might no message appear?
Django
from django.contrib import messages from django.shortcuts import render def my_view(request): messages.error(request, 'An error occurred') return render(request, 'my_template.html')
Attempts:
2 left
💡 Hint
Check if the messages middleware is enabled in the Django settings.
✗ Incorrect
If the messages middleware is missing, messages won't be stored or passed to the template, so no messages appear.
❓ state_output
advanced2:00remaining
What is the value of messages after this code runs?
Given this Django view code snippet:
from django.contrib import messages
def my_view(request):
messages.info(request, 'Info 1')
messages.warning(request, 'Warning 1')
messages.info(request, 'Info 2')
What will be the count of messages stored for the current request?
Django
from django.contrib import messages def my_view(request): messages.info(request, 'Info 1') messages.warning(request, 'Warning 1') messages.info(request, 'Info 2')
Attempts:
2 left
💡 Hint
Each call to messages adds one message to the storage.
✗ Incorrect
Each call to messages.info or messages.warning adds one message, so total is 3.
🧠 Conceptual
expert3:00remaining
Which statement about Django's messages framework is TRUE?
Select the true statement about how Django's messages framework works.
Attempts:
2 left
💡 Hint
Think about how messages persist between requests.
✗ Incorrect
By default, Django stores messages in the session, so session middleware is required to use messages.