我正在嘗試在我的 django 應用程式中使用會話變數
查看.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)
這在我的本地服務器上完美運行。
在遠程服務器上,context.room_name_create顯示一個值,但context.room_name_check在瀏覽器中顯示為null。
這只是一個小應用程式,我已將所有源檔案復制到遠程服務器,因此所有源檔案都是相同的。我已經在不同的瀏覽器上測驗過了
我已經在 shell 中運行了Django 教程中的示例,它運行良好
誰能建議我可以檢查什么來解決這個問題?
uj5u.com熱心網友回復:
要在 Django 中使用會話,您通常需要做兩件事:
設定會話中的資料
在您的情況下:room_name = request.session.set('room_name') = some_value或
room_name = request.session['room_name'] = some_value
從會話中獲取資料
在你的情況下: room_name = request.session.get('room_name')
注意:在嘗試檢索資料之前,您需要在會話中設定資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/368335.html
上一篇:django用戶身份驗證只回傳無
