Imtiaz Ahmed
Imtiaz Ahmed

Reputation: 323

Show messages using django messages framework from one particular view

I'm using Django messages framework to show a success message in my contact page template when contact form submission is succeed.

since installed app, middlewares and context processor for messages are set in settings.py all the messages that are being generated showing in my contact template, for instance, Login success, logout success, and so on.

I just want to show the message.success in the contact templete that I defined in contact view:

def contact(request):
    if request.method == 'POST':
        Contact.objects.create(
            first_name=request.POST.get('first_name', ''),
            last_name=request.POST.get('last_name', ''),
            email=request.POST.get('email', ''),
            subject=request.POST.get('subject', ''),
            message=request.POST.get('message', ''),
        )
        # I want to show only this message on 'POST' success of this view
        messages.success(request, "Your message has been submitted.")
    return render(request, 'contact/contact.html')

my template:

<div class="success-message">
    {% if messages %}
        <ul class="messages">
            {% for message in messages %}
                <li class="{{ message.tags }}">
                    <h4 class="alert-success text-center" style="padding-top: 5px; padding-bottom: 5px;">
                        {{ message }}
                    </h4>
                </li>
            {% endfor %}
         </ul>
     {% endif %}

</div>

my template is showing all the messages that are being generated throughout the website along with the one that I want. how to prevent other messages except the one that I want?

What is the workaround for this problem, Can somebody help me through this?

Upvotes: 0

Views: 3435

Answers (2)

We1337
We1337

Reputation: 1


# Should be on top of the function
def clear_messages(request):
    storage = messages.get_messages(request)
    storage.used = True
    
def contact(request):
    # This will clear all messages
    clear_messages(request)

    if request.method == 'POST':
        Contact.objects.create(`enter code here`
            first_name=request.POST.get('first_name', ''),
            last_name=request.POST.get('last_name', ''),
            email=request.POST.get('email', ''),
            subject=request.POST.get('subject', ''),
            message=request.POST.get('message', ''),
        )
        messages.success(request, "Your message has been submitted.")
    return render(request, 'contact/contact.html')

Django version: 5.1
Links to docs: https://docs.djangoproject.com/en/5.0/ref/contrib/messages/#expiration-of-messages

Upvotes: 0

iri
iri

Reputation: 744

You should clear the messages, then add your success message, then render the template.

def contact(request):
    if request.method == 'POST':
        ...
        list(messages.get_messages(request))
        messages.success(request, "Your message has been submitted.")
    return render(request, 'contact/contact.html')

Upvotes: 1

Related Questions