金字塔:如何在没有渲染器的情况下设置cookie?

Vit*_*mar 9 python pyramid

在配置文件中:

config.add_route('lang', '/lang-{code}')
Run Code Online (Sandbox Code Playgroud)

在视图中:

@view_config(route_name='lang')
def lang(request):
    code = request.matchdict['code']
    response = Response()
    response.set_cookie('lang', value=code, max_age=31536000) # max_age = year

    return HTTPFound(location=request.environ['HTTP_REFERER'])
Run Code Online (Sandbox Code Playgroud)

机制很简单:有一个带有语言的下拉菜单项,点击任何人都必须使用新的语言环境刷新站点.

运行没有错误,但没有设置cookie ...我做错了什么?

谢谢!

Ant*_*air 12

这个答案非常好.另一种选择是将HTTPFound实例用作Response:

@view_config(route_name='lang')
def lang(request):
    code = request.matchdict['code']
    response = HTTPFound(location=request.environ['HTTP_REFERER'])
    response.set_cookie('lang', value=code, max_age=31536000) # max_age = year

    return response
Run Code Online (Sandbox Code Playgroud)


Rob*_*ers 11

HTTPFound采用headers参数.尝试类似的东西return HTTPFound(location='foo', headers=response.headers)