简体   繁体   中英

writing to django session database table after configurating settings.py

I'm writing a new backend API using Django.

I have configured the following values in 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.

Here's simplistic example how to use request.session , taken directly from Django session documentation :

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!')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM