姜戈与斜纹布 - 未设置会话 cookie
我正在使用 twill 和 django-test-utils,它没有让我登录。
我认为它没有设置 django 会话 cookie。我转到一个调用 request.session.set_test_cookie()
的网页(斜纹),调用返回后,twill.show_cookies()
(应该打印出cookies),显示没有cookies。在另一个页面/视图中,request.session.test_cookie_worked()
返回False
。
奇怪,另一个页面调用request.set_cookie(...)
来手动设置cookie,并且有效。 twill.show_cookies()
显示该 cookie。所以一些 cookie 正在工作,但会话中间件似乎不起作用......
可能发生了什么?如何将 django 会话 cookie 设置为斜纹?
其他一些人也遇到了类似的问题,django-users 上的示例, 关于另一个 twill/django 脚本上的 cookie 的评论
I'm testing a django application with twill, and django-test-utils, and it's not logging me in.
I think it's not setting the django session cookie. I go to a webpage (in twill) that calls request.session.set_test_cookie()
, and after that call returns, twill.show_cookies()
(which should print out the cookies), shows no cookies. In another page/view, request.session.test_cookie_worked()
returns False
.
Oddly, another page, calls request.set_cookie(...)
to manually set a cookie, and that works. twill.show_cookies()
shows that cookie. So some cookies are working, but the session middleware just doesn't seem to be working...
What could be going on? How do I get the django session cookie to be set in twill?
Some other people have had similar problems, example on django-users, comment about cookies on another twill/django script
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 FC 所说,如果客户端和服务器是不同的机器,并且时间(甚至可能是时区)不匹配,则可能会遇到 cookie 过期问题。
我刚刚仔细检查了 < 的代码code>set_test_cookie(),看起来它设置该值非常正常。您确定遇到问题的页面不会返回错误(因此不会设置
test_cookie
)?可能发生的情况有很多种,但我突然想到:Host
标头referer
应用程序需要仔细检查您的其他中间件。尝试创建一个仅测试此问题的空白项目。一个仅启用管理员的空白项目就足够了。如果您可以在最小配置中复制它,我们可以更有效地追踪错误。
As F.C. said, if the client and server are different machines with non-matching times (or even possibly timezones), you may end up with cookie expiration problems.
I just double-checked the code for
set_test_cookie()
, and it looks like it sets that value pretty normally. Are you certain that the page you are having trouble with isn't returning an error (and thus not setting thetest_cookie
)? There are a whole bunch of ways that could happen, but off the top of my head:Host
header your app requiresreferer
your app requiresDouble-check your other middlewares. Try making a blank project that ONLY tests this issue. A blank project with just the admin enabled should be enough. If you can replicate it in a minimal configuration, we can be more helpful with chasing down the bug.