金字塔:如何在没有渲染器的情况下设置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'])
机制很简单:有一个带有语言的下拉菜单项,单击任何人都必须使用新的区域设置刷新站点。
运行没有错误,但没有设置cookie... 我做错了什么?
谢谢!
In configuration file:
config.add_route('lang', '/lang-{code}')
In views:
@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.
Runs without errors, but no cookie set up...
What I did wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HTTPFound 采用 headers 参数。尝试类似
return HTTPFound(location='foo', headers=response.headers)
HTTPFound takes a headers parameter. Try something like
return HTTPFound(location='foo', headers=response.headers)
这个答案非常好。另一种选择是使用
HTTPFound
实例作为Response
:This answer is excellent. Another option is usage of the
HTTPFound
instance as aResponse
:使用 render_to_response 设置 cookie
Set cookie by using render_to_response