[Answered ]-Javascript notification in django

2đź‘Ť

You can set cookies in response, when photo saved, and write some jquery code to get and show it, code like this:

...
newdoc.save()
response = render_to_response('myprofile/user_image_upload.html',{'uploaded_image':uploaded_image,'form':form},context_instance = RequestContext(request))

response.set_cookie('message', "your file upload has been done") 
return response

Jquery code to get this message from cookies with plugin https://github.com/carhartl/jquery-cookie, and show on page:

msg = $.cookie("message")
if (msg)
    alert(msg);

And you can choice a jquery notification plugin(such as http://ned.im/noty/) to replace “alert(msg)”.

👤user2647646

0đź‘Ť

A simple example accepting an ajax request might look something like this:

First modify your view to only accept ajax requests and send back a basic http response, otherwise refuse access. You dont need to render a full response.

def UserImageUpload(request):
    if request.method == 'POST' and request.is_ajax():
        ...
        [your form validation code]
        ...
        return HttpResponse('ok')
    else:
        return HttpResponseNotAllowed(['POST'])

And then have your javascript give a simple alert according to the response received from your server.

Im not sure how you’re handling AJAX, but if you’re using Jquery, these instructions might help: http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax

Then here’s some prepackaged ajax tools for Django: dajaxice: http://www.dajaxproject.com/

👤skzryzg

Leave a comment