1π
- setup the server side end point to accept the post request
- read this ajax api from jquery
ex:
$.ajax({
method: "POST",
url: "url/path/some",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
url: is the end point in server side.
data: json data that goes on body request.
method: POST http protocol.
simply as that.
You can call this ajax after the user click on button to send the image
1π
Using Ajax
means you make asynchronous javascript
calls to the server. This can be done via a javascript
script in-page or a linked file. The script would call the GET
method on your server url, which would return the changed data.
What you (probably) need is, every few time intervals,
- send a
GET
request to the server to check if there are any updates - if the server responds with an affirmation, send another
GET
request to receive those updates - update the elements with the updated information
There are two things you would need to develop here β
ONE: javascript code to send ajax
requests and update elements (using jQuery
since itβs of much help to make things easier)
/** attach to document load or ready state */
function update_stuff() {
$.get('/server_url_check_updates/', function(data ,status) {
if (data.has_updates === 'YES') {
/** send request to get updated data */
$.get('/server_url_get_updates/', function(data, status) {
/** update your elements */
}
}
}
}
setTimeout(update_stuff, interval_in_seconds);
TWO. server code that handles GET
requests
import json
import HttpRequest
# map to /server_url_check_updates/
def check_for_updates(request):
# see if it's a GET request
if request.GET:
has_updates = True # True/False depending on updates
return HttpResponse(
json.dumps({'has_updates': True, }),
content_type="application/json")
# map to /server_url_get_updates/
def send_updates(request):
if request.GET:
# create dictionary of your objects
dictionary_of_your_objects = {}
data = json.dumps(dictionary_of_you_objects)
# return them lumped together or as separate objects
return HttpResponse(
json.dumps({'updates': data, }),
content_type="application/json")
You can add additional code to check whether the GET
requests are valid, use only one url for both functions (check the GET
parameter to see what is being requested), etc. See the following links for more information on this:
js/jquery/ajax
javascript β ajax calls (w3schools)
jquery β ajax calls (w3schools)
django
- [Answered ]-Django β Override Default Manager in Admin β InheritanceManager
- [Answered ]-Using a dictionary (or other structure) key in a variable name