Last active
September 13, 2021 22:18
-
-
Save DmytroLitvinov/39d9a1a93a46eb9da1e17d8e73f35e11 to your computer and use it in GitHub Desktop.
[Django middleware for AJAX messages] Middleware for JSON responses. It adds to each JSON response array with messages from django.contrib.messages framework. It allows handle messages on a page with javascript. Django 1.10. #tags: django, python, ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="messages"> | |
{% for message in messages %} | |
<div {% if message.tags %}class="alert alert-dismissable alert-{{ message.tags }}"{% endif %}> | |
<a class="close" data-dismiss="alert" href="#">×</a> | |
{{ message }} | |
</div> | |
{% endfor %} | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import messages | |
import simplejson | |
from .utils import get_message_dict | |
class AjaxMessaging(object): | |
""" | |
Middlware for JSON responses. It adds to each JSON response array with | |
messages from django.contrib.messages framework. | |
It allows handle messages on a page with javascript | |
""" | |
def __init__(self, get_response): | |
self.get_response = get_response | |
# One-time configuration and initialization. | |
def __call__(self, request): | |
response = self.get_response(request) | |
if request.is_ajax(): | |
if response['Content-Type'] in ["application/javascript", | |
"application/json"]: | |
try: | |
content = simplejson.loads(response.content) | |
assert isinstance(content, dict) | |
except (ValueError, AssertionError): | |
return response | |
content['django_messages'] = [get_message_dict(message) for | |
message in | |
messages.get_messages(request)] | |
response.content = simplejson.dumps(content) | |
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
function addMessage(text, extra_tags) { | |
var message = $(` | |
<div class="alert alert-dismissable alert-${extra_tags}">\n | |
<a class="close" data-dismiss="alert" href="#">×</a>\n | |
${text}\n | |
</div>`).hide(); | |
$("#messages").append(message); | |
message.fadeIn(500); | |
setTimeout(function() { | |
message.fadeOut(500, function() { | |
message.remove(); | |
}); | |
}, 3000); | |
} | |
$(document).ajaxComplete(function (e, xhr, settings) { | |
var contentType = xhr.getResponseHeader("Content-Type"); | |
if (contentType == "application/javascript" || contentType == "application/json") { | |
var json = $.evalJSON(xhr.responseText); | |
$.each(json.django_messages, function (i, item) { | |
addMessage(item.text, item.tags); | |
}); | |
} | |
}).ajaxError(function (e, xhr, settings, exception) { | |
addMessage("There was an error processing your request, please try again.", "error"); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment