Using Django's Built-in Messaging System
Learn how to use Django's built-in messaging system to send messages between users.
Introduction to Django Messaging
Django comes with a built-in messaging system that allows you to send messages between users. In this tutorial, we will learn how to use Django's messaging system.
Step 1: Create a New Message
To start, you need to create a new message. You can do this by using the messages framework in Django. Add the following code to views.py:
from django.contrib import messages
from django.shortcuts import render
def my_view(request):
messages.info(request, 'This is a message.')
return render(request, 'my_template.html')
Step 2: Display the Message
After creating the new message, you need to display it to the user. You can do this by using the messages framework in Django. Add the following code to my_template.html:
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
Ready for more? These paid resources pick up where this lesson leaves off.
Project-based Django video courses — the perfect paid next step after these free lessons.
Browse on Udemy →Hand-picked Django books to master the fundamentals offline.
See on Amazon →Some links on this page are affiliate links: we may earn a commission at no extra cost to you. We only recommend tools we believe are genuinely useful.