Messages

Django Messages Framework

messages.html: create a new file called messages.html inside the root\templates directory..

settings.py: add the following line:

MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'

base.html: in your base.html file (or any other template that you want to display messages in), include the messages.html template using the following code:

views.py: import messages

from django.contrib import messages

Use the framework to create a message. For example, you can create an error message if the user is not the owner of some data:

Keep in mind Django AllAuth handles messages automaticaly and it isn't necessary to edit any temapltes.

Or you can use it to display a success message:

if forms_species_add.is_valid():
    new_species = forms_species_add.save(commit=False)
    new_species.species_owner = request.user
    new_species.save()
    messages.success(request, f"{new_species.species_name} added successfully")
    return redirect('homepage')