I want to update a Django session variable following a Javascript event (well, actually jQuery).
Do I need to do this via a POST request?
Or can Javascript and Django share session variables in some clever way, in which case can I update the session variables direct from jQuery? I'm a bit hazy on the details.
Thanks!
You can do this via Ajax. You'll need a simple Django view that updates the session variable, which the jQuery will call:
def update_session(request):
if not request.is_ajax() or not request.method=='POST':
return HttpResponseNotAllowed(['POST'])
request.session['mykey'] = 'myvalue'
return HttpResponse('ok')
and the JS:
$.post('/update_session/', function(data) {
alert(data);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With