molakalapalli ramesh
molakalapalli ramesh

Reputation: 217

How to travel session id to HTML pages

I am planing to travel my session id to all my HTML pages.Here i am creating session id and getting that using sessionId = request.session.session_key After this how to send my session id to all my HTML pages.Is there any example code then that would be better.

Thanks, Ram.

Upvotes: 0

Views: 199

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174622

The session is automatically available in all views. You don't need to send it in the query string:

Session IDs in URLs

The Django sessions framework is entirely, and solely, cookie-based. It does not fall back to putting session IDs in URLs as a last resort, as PHP does. This is an intentional design decision. Not only does that behavior make URLs ugly, it makes your site vulnerable to session-ID theft via the “Referer” header.

All you have to do, is make sure the session hasn't expired:

def home(request):
  foo = request.session.get('foo')
  if not foo:
     print('Oops! Session has expired. Do something')
  else:
     print('Session is still valid, value of foo is {}'.format(foo))
  return render(request, 'index.html', {'foo': foo})

Have a read of the documentation, sessions in django are very straightforward.

Upvotes: 1

Related Questions