Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyramid: how to set cookie without renderer?

Tags:

python

pyramid

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!

like image 367
Vitalii Ponomar Avatar asked Jan 05 '12 16:01

Vitalii Ponomar


1 Answers

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
like image 133
Antoine Leclair Avatar answered Sep 29 '22 12:09

Antoine Leclair