Python - Сообщения уведомлений
//First add the following codes to the LOWER part of the SETTINGS
from django.contrib.messages import constants as messages
MESSAGE_TAGS = {
messages.ERROR: 'danger', // only if you want to display error
}
// Second is do the following steps
// Open a new file inside INCLUDES folder inside templates folder
// and call it messages.html
// open it and paste the codes below
{% if messages %}
{% for message in messages %}
<div id="message" class="container">
<div class="alert alert-{{ message.tags }}" alert-dismissable role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span></button>
<strong>
{% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}
Error:
{% else %}
{{mesaage.tags|title}}
{% endif %}
</strong>
{{message}}
</div>
</div>
{% endfor %}
{% endif %}
// you can these code for bootstrap or django
// Third is, go to the CLASS folder and in views.py add the codes below
from django.contrib import messages
def register(request):
if request.method == 'POST':
messages.error(request, 'This is error message') // you add this line
return redirect('register')
else:
return render(request, 'pages/register.html')
// Forth is, go the place where you want to display the message
// So, go to the register.html and the paste the code below
{% include 'includes/messages.html' %}
// depending on where you want to display the message
// most likely it is place above the <form>
// Fith is follow the steps below
// go to the mainControl or ( project level directory ) folder.
// go to ( static ) folder got to ( js ) folder got to ( app.js )
// go to the buttom part and paste the code below
// use to fade the message after 4 seconds
setTimeout(function(){
$('#message').fadeOut('slow');
}, 4000)
// to to the ( terminal ) and the run the code below
python manage.py collectstatic
// lastly -->
// if it is not running
// go to ( view page source ) search for ( app.js ) click it
// go to the buttom part check the code was loaded
setTimeout(function(){
$('#message').fadeOut('slow');
}, 2000)
// if not press ( refresh ) serveral times until you see it displayed.
BenLeolam