[Fixed]-Error msg to user in django


You can use the messaging framework in order to display a message on your page:

Quite commonly in web applications, you need to display a one-time notification message (also known as “flash message”) to the user after processing a form or some other types of user input.

For this, Django provides full support for cookie- and session-based messaging, for both anonymous and authenticated users. The messages framework allows you to temporarily store messages in one request and retrieve them for display in a subsequent request (usually the next one). Every message is tagged with a specific level that determines its priority (e.g., info, warning, or error).

There is extensive documentation and examples on the link provided.

EDIT: Example

Summary of settings (settings.py):



        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR,  'templates'),
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
            'debug': True,

In a view:

from django.contrib import messages

if a < b:
    messages.warning("a is less than b.")

In a template (example using bootstrap3):

{% if messages %}
    <div class="messages">
        {% for message in messages %}
            <div class="alert alert-{{ message.tags }} alert-dismissible" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                {{ message }}
        {% endfor %}
{% endif %}

Leave a comment