0

我使用DjangowithMongoDB作为后端数据库。

settings.py我已经设置了这些代码:

SESSION_ENGINE = 'mongoengine.django.sessions'

'django.contrib.sessions.middleware.SessionMiddleware',

'django.contrib.sessions',

在我的view/user.py(仅部分代码)中

                 if user.password == password:
                    #add session information
                    if request.session.test_cookie_worked():
                        returnmsg = "COOKIE OK"
                    else:
                        returnmsg = "COOKIE ERR"
                    response = HttpResponse(returnmsg)
                    response.set_cookie("username", username)
                    request.session['username'] = username
                    user.log.append(UserLog(time=datetime.now(), ip=request.META['REMOTE_ADDR'], login=True))
                    user.save()
                    return response

当我运行我的网站时,我可以通过此功能登录,其他需要验证我的会话设置的功能可以usernameresponse.session.

但是 returnmsg 是COOKIE ERR,当我使用 Safari 的 Web Inspector 时,我在这里看不到任何会话或 cookie。

Django 将会话数据存储在哪里?它应该是客户端计算机上的编码cookie吗?如果它没有存储在我客户的计算机上,为什么Django仍然可以得到它?在我的代码中,我也试过直接设置一个cookie,它仍然不起作用。

有什么不对MongoEngine吗?我是否以错误的方式使用它?

4

1 回答 1

1

You should set a test cookie first with set_test_cookie(), and then on a subsequent request check for it with test_cookie_worked().

The function doesn't test if cookies work, it checks specifically if the test cookie was successfully set - which has a consequence of checking if cookies work.

Unless you use set_test_cookie(), test_cookie_worked() will always fail.

于 2012-05-09T05:07:28.620 回答