根据这个示例,我在Django中扩展了会话。简而言之,我添加了一个名为account_id
的字段来保存会话所属用户的用户ID。
一切都很顺利,但我添加的自定义字段account_id
在登录时没有被设置。要设置它:
from django.contrib import auth
from MyApp.models import CustomSession
def login(request):
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
try:
auth.login(request, user)
session_key = request.session.session_key
# CODE HERE
if session_key is not None:
return HttpResponse(json.dumps({'status': 'Success'}))
else:
return HttpResponse(json.dumps({'status': 'Fail'}))
我已经尝试将以下内容放入
CODE HERE
,但都没有起作用:
request.session.model.account_id = user.id
session = CustomSession.objects.get(pk=session_key) session.account_id = user.id session.modified = True
request.session.account_id = user.id
request.session[account_id] = user.id
account_id
都是NULL
。我在这里漏掉了什么?