pythondjangoformsbootstrap-4django-messages

Django messages not showing up on contact form template


I have a contact form on my Django Site. I am attempting to have a message box popup when the user clicks to submit their message. For some reason, I cannot get the message I am creating to display. I have looked everywhere but cannot seem to find a solution.

I have ensured all steps to enable messages in the docs have been met. https://docs.djangoproject.com/en/3.0/ref/contrib/messages/

Any help suggestions would me much appreciated!

views.py

from django.shortcuts import render

# add to the top
from .forms import ContactForm

# new imports that go at the top of the file
from django.core.mail import EmailMessage
from django.shortcuts import redirect
from django.template.loader import get_template
from django.contrib import messages
import global_admin

nickname = global_admin.nickname

# our view
def contact(request):
    form_class = ContactForm

    # new logic!
    if request.method == 'POST':
        form = form_class(data=request.POST)

        if form.is_valid():
            contact_name = request.POST.get(
                'contact_name'
            , '')
            contact_email = request.POST.get(
                'contact_email'
            , '')
            form_content = request.POST.get('content', '')

            # Email the profile with the
            # contact information
            template = get_template('contact/contact_template.txt')
            context = {
                'contact_name': contact_name,
                'contact_email': contact_email,
                'form_content': form_content,
            }
            content = template.render(context)

            email = EmailMessage(
                "New contact form submission",
                content,
                "email@email.com",
                ['email@email.com'],
                headers={'Reply-To': contact_email }
            )
            email.send()
          


          
    messages.debug(request, 'Thanks for reaching out! We will be in contact soon!','')
    print(messages.get_messages(request))
    return render(request, 'contact/contact.html', {'form': form_class, 'nickname': nickname, 'messages': messages.get_messages(request)})

My template, contact.html

{% extends 'homepage/base.html' %}


<!--{% block title %}Contact - {{ block.super }}{% endblock %}-->

<!--this loads all the static items-->
{% load static %}

{% block content %}

{% load crispy_forms_tags %}

{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}

<div id="contact-div">

    <h1>Contact</h1>
    <form role="form" action="" method="POST" class="bootstrap4">
        {% csrf_token %}
        {{ form|crispy }}
        <button type="submit">Submit</button>
    </form>

</div>



{% endblock %}

Solution

  • Try this:

    messages.add_message(request, messages.INFO, 
       'Thanks for reaching out! We will be in contact soon!', 
        extra_tags='ex-tag')
    
    return render(request, 'contact/contact.html', {'form': form, 'nickname': nickname)