[英]writing to django session database table after configurating settings.py
I'm writing a new backend API using Django. 我正在使用Django编写一个新的后端API。
I have configured the following values in settings.py : 我在settings.py中配置了以下值:
CACHES = {
'default': {
'BACKEND': 'django.contrib.sessions.backends.cached_db'
}
}
SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'
I can't figure out how to write the session properties to the database. 我无法弄清楚如何将会话属性写入数据库。 Anyone have any idea? 任何人都有任何想法?
You have to activate also django.contrib.sessions.middleware.SessionMiddleware
middleware first to have access to request.session
dictionary in your views. 您还必须首先激活django.contrib.sessions.middleware.SessionMiddleware
中间件才能访问视图中的request.session
字典。
Here's simplistic example how to use request.session
, taken directly from Django session documentation : 以下是直接从Django 会话文档中获取的如何使用request.session
的简单示例:
def post_comment(request, new_comment):
if request.session.get('has_commented', False):
return HttpResponse("You've already commented.")
c = comments.Comment(comment=new_comment)
c.save()
request.session['has_commented'] = True
return HttpResponse('Thanks for your comment!')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.