Home > Blockchain >  Django Sessions not Working on remote server
Django Sessions not Working on remote server

Time:11-28

I am trying to use a session variable in my django app

view.py

def get(self, request):
    room_name = request.session.get('room_name')
    context = app.get_context(room_name)
    context['room_name_create'] = room_name
    context['room_name_check'] = request.session.get('room_name')
    return render(request, self.template, context)

js

console.log('a', context.room_name_create)
console.log('b', context.room_name_check)

This works perfectly on my local server.

On the remote server context.room_name_create shows a value, but context.room_name_check shows as null in the browser.

This is only a small app and I have copied all of the source files to the remote server and so all of the source is identical. I have tested this on different browsers

I have run the examples in the Django tutorial in the shell and it works perfectly

Can anyone suggest what I can check to resolve this?

CodePudding user response:

To work with session in Django, you need gerneraly need to do 2 times:

Set the data in the session In your case : room_name = request.session.set('room_name') = some_value or room_name = request.session['room_name'] = some_value

Get the data from the session In your case : room_name = request.session.get('room_name')

Note : You need to set data first in the session before try to retrieve it.

  • Related