Why isn't it easier to clear messages?

What I noticed is that the Django message is automatically cleared when you iterate. With my environment, I use sweet alert to show errors in my base file. So what I did is at the end of my base file, I created a script tag, created a variable mess, looped through messages, and appended each error message to the mess variable, After the loop, I use my js function to show the error message. my code example is below

{% if messages %}

{% for message in messages %}

            <script>
                mess += " {{ message }} \n"
            </script>

        {% endfor %}
        <script>
            error_handler(`error%%${mess}`)
        </script>

{% endif %}