Skip to content
Myr

Improved cookie documentation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent ae00f6d · Browse files · 1 file+32 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+32 added−5 removed

  • other:+32 added−5 removed

1 file+32 added−5 removed

modified:docs/quickstart.rst+32 added-5 removed
586 unmodified lines
587588Cookies589```````590591To access cookies you can use the :attr:`~flask.request.cookies`592attribute. Again this is a dictionary with all the cookies the client593transmits. If you want to use sessions, do not use the cookies directly594but instead use the :ref:`sessions` in Flask that add some security on top595of cookies for you.591To access cookies you can use the :attr:`~flask.Request.cookies`592attribute. To set cookies you can use the593:attr:`~flask.Response.set_cookie` method of response objects. The594:attr:`~flask.Request.cookies` attribute of request objects is a595dictionary with all the cookies the client transmits. If you want to use596sessions, do not use the cookies directly but instead use the597:ref:`sessions` in Flask that add some security on top of cookies for you.598599Reading cookies::600601 from flask import request602603 @app.route('/')604 def index():605 username = request.cookies.get('username')606 # use cookies.get(key) instead of cookies[key] to not get a607 # KeyError if the cookie is missing.608609Storing cookies::610611 from flask import make_response612613 @app.route('/')614 def index():615 resp = make_response(render_template(...))616 resp.set_cookie('username', 'the username')617 return resp618619Note that cookies are set on response objects. Since you normally you620just return strings from the view functions Flask will convert them into621response objects for you. If you explicitly want to do that you can use622the :meth:`~flask.make_response` function and then modify it.623624625Redirects and Errors626--------------------627628To redirect a user to somewhere else you can use the
148 unmodified lines