Search code examples
pythonpython-3.xpyramidpylons

Python Pylons/Pyramid Cookies


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'

Solution

  • 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.