Skip to main content
 首页 » 编程设计

Python Pylons/Pyramid cookies

2024年12月31日10bhlsheji

在 Pylons/Pyramid 中设置和获取 cookie 的最佳方式是什么?

Response.set_cookie('example_cookie_name', 'example', max_age=180*24*3600) 

返回错误

  File "/usr/local/lib/python3.5/dist-packages/webob/response.py", line 1071, in set_cookie 
    self.headerlist.append(('Set-Cookie', cookie))  
AttributeError: 'str' object has no attribute 'headerlist' 

请您参考如下方法:

你似乎在做这样的事情:

from pyramid.response import Response 
Response.set_cookie('example_cookie_name', 'example', max_age=180*24*3600) 

问题是 Response 是一个类,您调用它的未绑定(bind)方法 set_cookie 传递一个字符串代替 self 参数.

(有趣的事实 - 在 Python 2 中错误是 much clearer )

您需要实例化一个新的响应对象或简单地使用 request.response 属性。