Django messages framework not displaying message?
<p>I'm trying to build a community portal and am working on displaying one-time message to the user such as "login successful" and the like; and am working with Django's messages framework. </p>

<p>My template has the following line which currently does nothing:</p>

<pre><code>{% if messages %}{% for message in messages %}&lt;script&gt;alert({{ message }});&lt;/script&gt;{% endfor %}{% endif %}
</code></pre>

<p>Strangely, each of the following works:</p>

<pre><code>{% if messages %}{% for message in messages %}&lt;script&gt;alert();&lt;/script&gt;{% endfor %}{% endif %}

{% if messages %}{% for message in messages %}&lt;script&gt;alert("Welcome");&lt;/script&gt;{% endfor %}{% endif %}
</code></pre>

<p>From this, I am concluding that I'm not creating, storing, or passing the messages correctly. However, I'm checking the docs and my syntax seems fine.</p>

<p>My message creation; views.py:</p>

<pre><code>def login(request):

    userName = request.POST.get('usrname',None)
    userPass = request.POST.get('psw',None)

    user = authenticate(username=sanitize_html(userName), password=userPass)

    if user is not None:
        if user.is_active:
            auth.login(request, user)
        messages.add_message(request, messages.INFO, 'Successfully logged in!')
    else:
        messages.add_message(request, messages.INFO, 'Login not successful. Please try again.')

    return HttpResponseRedirect('/home/')
</code></pre>

<p>My message retrieval and passing, views.py (maps to url '/home/'):</p>

<pre><code>def test(request):

    messagealert = []
    mess = get_messages(request)

    for message in mess:
        messagealert.append(message)

    if request.user.is_authenticated():
            student_user = get_student_user(request.user)
            student_user.first_name = request.user.first_name
            student_user.last_name = request.user.last_name
            student_user.email = request.user.email

            content = {
                'student_user': student_user,
                'messages': messagealert,
            }
    else:
            content = {
                'student_user': None,
                'messages': messagealert,
            }

    return render_to_response('index3.html', content)
</code></pre>

<p>My index3.html template is the template with the line given above.</p>

<p>What am I missing here?</p>

<p>Don't use <code>render_to_response</code> in your <code>test</code> view. It doesn't run context processors which are required to insert things like <code>messages</code> - and other useful items such as <code>user</code> - into the context.</p>

<p>Use <code>render</code> instead:</p>

<pre><code>return render(request, 'index3.html', context)
</code></pre>

python django authentication authorization django-messages