金字塔:如何在没有渲染器的情况下设置 cookie? [英] Pyramid: how to set cookie without renderer?

查看:50
本文介绍了金字塔:如何在没有渲染器的情况下设置 cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在配置文件中:

config.add_route('lang', '/lang-{code}')

在视图中:

@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'])

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

The mechanism is simple: there is a dropped down menu item with languages and clicking on anyone must refresh site with new locale.

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

Runs without errors, but no cookie set up... What I did wrong?

谢谢!

推荐答案

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

HTTPFound takes a headers parameter. Try something like return HTTPFound(location='foo', headers=response.headers)

这篇关于金字塔:如何在没有渲染器的情况下设置 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆