l1dge / techswap

Repo for the techswap project
2 stars 0 forks source link

Use the Django messages framework #52

Open bbelderbos opened 3 years ago

bbelderbos commented 3 years ago

Example from our platform:

base.html

    <div class="mui-row">
      <div class="mui-col-sm-10 mui-col-sm-offset-1" id="messages">
        {% for message in messages %}
          <p style="color: {% if message.tags == 'success' %}green{% elif message.tags == 'error' %}red{% endif %}">
            {% if 'safe' in message.tags %}{{ message|safe }}{% else %}{{ message }}{% endif %}
          </p>
        {% endfor %}
      </div>
    </div>

In views you can use messages.success(request, "your message") and messages.error(request, "your message") and it will display in the html pasted above.

More info: https://docs.djangoproject.com/en/3.2/ref/contrib/messages/

HTH Bob