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

发布于 2024-12-25 08:42:15 字数 478 浏览 1 评论 0原文

在配置文件中:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故笙诉离歌 2025-01-01 08:42:15

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

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

丶视觉 2025-01-01 08:42:15

这个答案非常好。另一种选择是使用 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

This answer is excellent. Another option is usage of the HTTPFound instance as a 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
弄潮 2025-01-01 08:42:15

使用 render_to_response 设置 cookie

@view_config(route_name='index')  
def index_view(request):  
    lang = 'bn' 
    template = '../templates/index.jinja2'
    request.response.set_cookie('lang', lang) 
    return render_to_response(template, {'user_id':user_id}, request=request)

Set cookie by using render_to_response

@view_config(route_name='index')  
def index_view(request):  
    lang = 'bn' 
    template = '../templates/index.jinja2'
    request.response.set_cookie('lang', lang) 
    return render_to_response(template, {'user_id':user_id}, request=request)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文