Python Pylons/Pyramid Cookies

714 Views Asked by At

What is the best way to set and get cookies in pylons/pyramid?

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

returns the error

  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'
1

There are 1 best solutions below

2
Sergey On BEST ANSWER

You seem to be doing something like this:

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

The problem is that Response is a class and you're calling its unbound method set_cookie passing a string in place of the self argument.

(Fun fact - in Python 2 the error is much clearer)

You need to either instantiate a new response object or simply use the request.response attribute.