0
0
Djangoframework~5 mins

Messages framework for flash messages in Django - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of Django's Messages framework?
It allows you to store one-time notifications (flash messages) that can be displayed to users after a page reload or redirect.
Click to reveal answer
beginner
How do you add a message in a Django view?
Use the messages module, for example: messages.success(request, 'Your profile was updated.').
Click to reveal answer
intermediate
Name the default message levels provided by Django's Messages framework.
DEBUG, INFO, SUCCESS, WARNING, ERROR.
Click to reveal answer
beginner
How do you display messages in a Django template?
Use a loop over messages context variable, for example:<br>
{% raw %}{% if messages %}
  {% for message in messages %}
    <div class="{{ message.tags }}">{{ message }}</div>
  {% endfor %}
{% endif %}{% endraw %}
Click to reveal answer
intermediate
What middleware must be enabled for Django's Messages framework to work?
The django.contrib.messages.middleware.MessageMiddleware must be included in the MIDDLEWARE setting.
Click to reveal answer
Which function adds a success message in Django?
Amessages.success(request, 'Message')
Bmessages.error(request, 'Message')
Cmessages.info(request, 'Message')
Dmessages.warning(request, 'Message')
Where do you typically display flash messages in Django?
AIn the template using the messages context variable
BIn the database admin panel
CIn the model
DIn the view function
Which middleware is required for Django messages to work?
ASessionMiddleware
BCsrfViewMiddleware
CAuthenticationMiddleware
DMessageMiddleware
What happens to messages after they are displayed once?
AThey are archived
BThey stay forever
CThey are deleted automatically
DThey are sent by email
Which message level is NOT part of Django's default levels?
ADEBUG
BCRITICAL
CWARNING
DERROR
Explain how to add and display a flash message in Django using the Messages framework.
Think about the flow from view to template and middleware role.
You got /4 concepts.
    List and describe the default message levels in Django's Messages framework.
    Levels indicate message importance and style.
    You got /5 concepts.