Last active
September 13, 2021 22:18
Revisions
-
Dmytro Litvinov revised this gist
Feb 10, 2017 . 2 changed files with 39 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ <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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ $(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"); }) }); -
Dmytro Litvinov revised this gist
Feb 9, 2017 . No changes.There are no files selected for viewing
-
Dmytro Litvinov created this gist
Feb 9, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ 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