I would like to know how could I use a session variable to define a message and display it on the next page.
I have created a base template that I extend in all templates. I inserted the modal message code to check if there is any message in the base template
My base template(base.html)
...
{% if request.session.message %}
<div id="modalAlert">
<div class="modal-backdrop"></div>
<div class="modal" id="myModal">
<div class="modal-header">
<button class="close" data-dismiss="modal">
×
</button>
<h3>{{ request.session.title }}</h3>
</div>
<div class="modal-body">
<p>
{{ request.session.message }}
</p>
</div>
<div class="modal-footer">
<a href="#" id="botao-fechar" class="btn">Ok</a>
</div>
</div>
</div>
<script type="text/javascript">
$('#modalAlert').show();
$('#myModal button.close,#myModal #botao-fechar').click(function() {
$('#modalAlert').hide();
})
</script>
{% endif %}
...
And in my view I have inserted the message in session
def home(request):
request.session.message = 'Test message'
context_instance = RequestContext(request)
return render_to_response("professor/home.html", context_instance)
However doesn't appear the message, there is nothing in the session. I have tested the modal and it is okay. What I am doing wrong?
You need to add django.core.context_processors.request to your TEMPLATE_CONTEXT_PROCESSORS
settings so you can access request from template, default settings does not include this, so your message won't show.
Why not use Django default message framework?