[Fixed]-Update Django session variable in javascript?

28👍

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);
});

Leave a comment