Bharath Kumar
Bharath Kumar

Reputation: 850

Django session variable in JavaScript

Can i use Django session variable in JavaScript?

In django:

request.session['email'] = '[email protected]'

In JS:

sessionStorage.getItem("email")

Upvotes: 2

Views: 6622

Answers (1)

askvictor
askvictor

Reputation: 3819

You could create a simple view:

class SessionVarView(TemplateView)
    def get(self, request, *args, **kwargs):
        return HttpResponse(request.session[kwargs['key']])

and in urls.py add:

url(r'^session/(?P<key>[^/]+)$', SessionVarView.as_view(), name='session-var'),

Then you could get it from JS via http://your.site.address/session/key_name_here or something similar.

(note - this is from the top of my head, and is not tested)

Upvotes: 1

Related Questions