0

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?

Imtiaz Ahmed
  • 123
  • 3
  • 12
  • It happens when you don't define `next` page to redirect to, `messages` are redirected to just next page or route,if you are saying all other views' messages are also being displayed with my `post` message, that means your all messages found their next page, to your `contact/contact.html` which is in your `contact` view. – Sunderam Dubey Apr 09 '22 at 20:45
  • @SunderamDubey All the required settings for messages context to be available in every template are set in settings.py by default. for that reason if I use (for message in messages ) in any of the template it will show any messages that are being created. My objective here is to show only the messages that are being generated after contact form submission in the 'contact/contact.html' template. – Imtiaz Ahmed Apr 09 '22 at 20:56

1 Answers1

0

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')
iri
  • 735
  • 4
  • 8
  • Tried this way. But It's still showing every other messages. – Imtiaz Ahmed Apr 10 '22 at 08:15
  • Well I've figured out another way, which is not using django-messages, I declared a custom_messages variable with my custom message and passed it into the context. For now it will do, But if you have any solution with django-messages please let me know – Imtiaz Ahmed Apr 10 '22 at 08:51
  • Ok, I think that my answer does not work in recent versions of Django, try `storage = messages.get_messages(request)` and then `storage.used = True` instead, that should work instead of `list(messages.get_messages(request))` – iri Apr 10 '22 at 17:50
  • Thanks for your time mate. That too is the same. I think we need to write custom middleware for django-messages, to handle the response coming from a particular view, to show the messages declared in that particular view. Other than that it's gonna show all the messages if we use 'messages' in any template. I'm not sure, but it might be a solution. – Imtiaz Ahmed Apr 10 '22 at 18:36