Update Django session variable in javascript?

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!

Answers


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

Need Your Help

NUnit TestCase with Generics

c# unit-testing generics nunit testcase

Is there any way to pass generic types using a TestCase to a test in NUnit?

Custom HTML tag attributes are not rendered by JSF

html jsf custom-attributes renderer

I want to add some iOS specific tag attributes to my login-form. If I have a look on my web page source, the attributes autocorrect, autocapitalize and spellcheck aren't there. What is the reason for