#### Repository https://github.com/python #### What Will I Learn? - Store cookie in Flask - Get a cookie and render in the template #### Requirements - Basic Python - Install Python 3 - Install Flask #### Resources - Python - https://www.python.org/ - Flask - http://flask.pocoo.org/ - Jinja2 -http://jinja.pocoo.org/docs/2.10/ #### Difficulty Basic ### Tutorial Content This tutorial is a continuation of the tutorial series on building web applications with python. so for those of you who don't have basic about python, I suggest you follow the previous tutorial in the curriculum below. In the previous [tutorial](https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-3-get-method-query-parameter-and-navigate-routing-1543411189705), we have learned the *get method* and about *url_for*. In this tutorial, we will learn *cookies* on the flask and we will also make a login with the flask using the session. ### Cookie in Flask On a website a cookie is often used to store data on a client computer. *cookies* are often used to store web data into our client's computer, so that when we visit the web for the first time and so on the web is still in the same state. in this section we will learn how to use cookies on the web application that we make with flasks, In the previous tutorial we created a routing ```/login``` that have the get and post method: ``` @app.route('/login', methods=["GET", "POST"]) def login(): if request.method == "POST": return "Your email is "+ request.form['email'] return render_template('login.html') ``` - **Use the ```make_response``` library** To make a Cookie on the flask we need help from the```make_response``` library. When installing flask we have also installed **make_response** automatically. but we need to import it as follows: **app.py** ``` from flask import Flask, render_template, request, make_response @app.route('/login', methods=["GET", "POST"]) def login(): if request.method == "POST": return "Your email is "+ request.form['email'] return render_template('login.html') ``` <br> **import library make_response** to use cookies on the flask we need tools from the ```make_response``` library. This library is automatically installed when we install the flask. All we need to do is import it. to import it we can do it like the following: **app.py** ``` from flask import Flask, render_template, request, make_response ``` After we import it we can use the library to make cookies as follows: ``` @app.route('/login', methods=["GET", "POST"]) def login(): if request.method == "POST": resp = make_response("Your email is "+ request.form['email']) resp.set_cookie('email_user', request.form['email']) return resp return render_template('login.html') ``` - After we have imported the **make_response** library now we can use it like this ```make_response ()```. This function is used to make a response based on the parameters that we passed to this function. In this tutorial, we will respond to ```'email_user', request.form ['email']```. We will give a response from the email we have 'POST' by input, you can see the explanation in this [tutorial](https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-2-templating-jinja2-and-method-post-on-routing-system-1542987551736). - After we made a response, now we can make a cookie from the response from the ```make_response()``` function. We can set cookies like this ```resp.set_cookie('email_user', request.form['email'])```. to set cookies we need to make a combination of keys and values, ```'email_user'``` is the **key** and ```request.form['email']``` is the **value**. - And I will return ```return resp```, for more details we can see examples like the picture below:  We can see in the picture above, cookies have been stored in our browser. *Cookies* are not always saved, because cookies have an expiration period. <br> - **get cookie** We have succeeded to save the cookies, of course, we don't just want to save the cookie, we also want to use it and get the value. in this section, we will do it. we will switch to route ```'/welcome'```, so we can see the used cookie on different pages. **app.py** ``` @app.route('/welcome') def homeFunction(): email = request.cookies.get('email_user') return render_template('index.html', email = email) ``` - We can use the request method to retrieve all ```request``` data. Because we want to get a cookie, we can use its function on ```request.cookies``` and we can use ```get()``` to get the cookie with the key of the cookie. - to get the cookie value we can use the function ```request.cookies.get()```, to get the value we can pass the key of the cookie we want to get. 'email_user' is the key of the cookie, You can see the picture below:  - We will try to show the value of the user's email cookie to another page, we will pass the value on page **index.html** ```return render_template('index.html', email = email)```. We pass the data with variable **email**. the following is the contents of index.html: **index.html** ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Template Jinja2</title> <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css')}}"> </head> <body> <a href="{{ url_for('homeFunction')}}">Home</a> <a href="{{ url_for('profFunc')}}">Profile</a> <a href="{{ url_for('login')}}">Login</a> <h1 class="title">Welcome to my profile {{email}}</h1> </body> </html> ``` We can print out the data like this ```{{email}}```and we can see the example as shown below:  We can see in the picture above we has been successful in retrieving the value of cookies stored in our browser, cookies are used to store data that is not sensitive or dangerous. We have learned how to use cookies on flasks based on python, Next tutorial I will discuss about the login system on the flask. I hope this tutorial can help you, see you in the next tutorial. thank you #### Curriculum [Web developement with python #1 : Flask initialization and Routing system](https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-1-flask-initialization-and-routing-system-1542726589553) [Web development with python #2 : Templating jinja2 and Method POST on routing system](https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-2-templating-jinja2-and-method-post-on-routing-system-1542987551736) [Web development with python #3 : Get method, Query parameter and Navigate Routing](https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-3-get-method-query-parameter-and-navigate-routing-1543411189705) #### Proof of work done https://github.com/milleaduski/python-web-app
author | duski.harahap | ||||||
---|---|---|---|---|---|---|---|
permlink | web-development-with-python-5-store-cookie-and-get-cookie-in-template-1543593496200 | ||||||
category | utopian-io | ||||||
json_metadata | {"app":"steemit/0.1","format":"markdown","image":["https://ipfs.busy.org/ipfs/Qmf5MVzcd52WKvW8x8V1nP4qgvw4fQyZcRp3nEJUgNDNeX","https://ipfs.busy.org/ipfs/QmYqbhyMviVNcv81x2aRkQc8kotmb871RMNZMfng47gmyy","https://ipfs.busy.org/ipfs/QmQZ9wbkfNnfgCh3gGVK76KdVEZRCRyqTPTPqxVezJR7Lb"],"tags":["utopian-io","tutorials","python","flask","web"],"links":["https://github.com/python","https://www.python.org/","http://flask.pocoo.org/","http://jinja.pocoo.org/docs/2.10/","https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-3-get-method-query-parameter-and-navigate-routing-1543411189705","https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-2-templating-jinja2-and-method-post-on-routing-system-1542987551736","https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-1-flask-initialization-and-routing-system-1542726589553","https://github.com/milleaduski/python-web-app"]} | ||||||
created | 2018-11-30 15:58:18 | ||||||
last_update | 2018-12-01 03:45:24 | ||||||
depth | 0 | ||||||
children | 3 | ||||||
last_payout | 2018-12-07 15:58:18 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 12.333 HBD | ||||||
curator_payout_value | 3.930 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 7,050 | ||||||
author_reputation | 60,094,717,098,672 | ||||||
root_title | "Web development with python #4 : Store cookie and Get cookie in template" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 100,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 76,155,629 | ||||||
net_rshares | 28,192,290,042,699 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
arcange | 0 | 34,067,449,492 | 4% | ||
raphaelle | 0 | 2,117,820,381 | 4% | ||
penguinpablo | 0 | 393,249,489,550 | 17% | ||
eforucom | 0 | 19,699,464,036 | 1% | ||
miniature-tiger | 0 | 102,181,056,644 | 50% | ||
jga | 0 | 1,705,163,793 | 10.36% | ||
yehey | 0 | 8,336,395,468 | 10% | ||
mercadosaway | 0 | 1,387,413,771 | 100% | ||
codingdefined | 0 | 7,731,940,791 | 7.5% | ||
bachuslib | 0 | 21,327,954,094 | 100% | ||
steemitri | 0 | 134,736,078,338 | 100% | ||
accelerator | 0 | 20,614,246,983 | 1.5% | ||
piaristmonk | 0 | 5,330,362,838 | 100% | ||
mcfarhat | 0 | 9,829,397,731 | 12.2% | ||
pataty69 | 0 | 8,463,381,227 | 25% | ||
tixinhacapitinha | 0 | 5,306,147,285 | 25% | ||
utopian-io | 0 | 26,935,747,497,341 | 20.72% | ||
scipio | 0 | 47,508,607,843 | 20% | ||
greenorange | 0 | 548,670,869 | 100% | ||
mvanyi | 0 | 2,553,227,923 | 100% | ||
dedicatedguy | 0 | 166,569,815,284 | 100% | ||
amosbastian | 0 | 67,162,795,539 | 30.52% | ||
tdre | 0 | 16,713,705,831 | 100% | ||
portugalcoin | 0 | 3,533,419,031 | 15% | ||
magpielover | 0 | 88,509,563 | 100% | ||
viperblckz | 0 | 4,102,718,366 | 100% | ||
micaelacf | 0 | 950,239,110 | 25% | ||
properfraction | 0 | 750,903,127 | 100% | ||
ezravandi | 0 | 464,005,700 | 2.29% | ||
hackerzizon | 0 | 154,471,544 | 1% | ||
effofex | 0 | 90,065,090 | 0.75% | ||
enlighted | 0 | 78,571,528 | 25% | ||
ryuna.siege | 0 | 208,957,451 | 100% | ||
czciborj | 0 | 484,930,136 | 100% | ||
sidorovaval | 0 | 484,745,659 | 100% | ||
jailueroterg | 0 | 522,293,373 | 100% | ||
prohunonir | 0 | 511,093,463 | 100% | ||
herznactvifor | 0 | 509,805,928 | 100% | ||
luc.real | 0 | 221,340,964 | 100% | ||
nieloagranca | 0 | 3,565,036,683 | 8% | ||
katherinec8 | 0 | 532,366,800 | 100% | ||
avaa620dthomas | 0 | 531,866,824 | 100% | ||
tbtek | 0 | 751,674,751 | 25% | ||
riftlustful | 0 | 485,232,073 | 100% | ||
duarte9sousa | 0 | 2,106,717,277 | 2.5% | ||
munhenhos | 0 | 1,723,619,369 | 25% | ||
mackenzie57 | 0 | 522,781,083 | 100% | ||
groovylarge | 0 | 485,834,736 | 100% | ||
bhaski | 0 | 1,975,769,668 | 25% | ||
meaninglathered | 0 | 485,796,374 | 100% | ||
maduresway | 0 | 535,778,490 | 100% | ||
melamoonspe | 0 | 535,461,343 | 100% | ||
momarsijit | 0 | 532,702,753 | 100% | ||
sappmylangdi | 0 | 537,794,926 | 100% | ||
merlin7 | 0 | 77,444,001,746 | 3.05% | ||
votes4minnows | 0 | 468,870,377 | 3% | ||
nfc | 0 | 20,833,552,738 | 2% | ||
hdu | 0 | 470,485,006 | 1.5% | ||
curbot | 0 | 2,185,583,876 | 100% | ||
vladlenkonkol | 0 | 488,929,151 | 100% | ||
plaguedelta | 0 | 488,925,885 | 100% | ||
plinalacrosse | 0 | 488,900,073 | 100% | ||
bluesniper | 0 | 47,513,770,731 | 3% | ||
peerzadaaabid | 0 | 554,436,881 | 100% |
Hey, @duski.harahap! **Thanks for contributing on Utopian**. Weβre already looking forward to your next contribution! **Get higher incentives and support Utopian.io!** Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)). **Want to chat? Join us on Discord https://discord.gg/h52nFrV.** <a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
author | utopian-io |
---|---|
permlink | re-web-development-with-python-5-store-cookie-and-get-cookie-in-template-1543593496200-20181205t054534z |
category | utopian-io |
json_metadata | "{"app": "beem/0.20.9"}" |
created | 2018-12-05 05:45:36 |
last_update | 2018-12-05 05:45:36 |
depth | 1 |
children | 0 |
last_payout | 2018-12-12 05:45:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 595 |
author_reputation | 152,955,367,999,756 |
root_title | "Web development with python #4 : Store cookie and Get cookie in template" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 76,382,261 |
net_rshares | 13,366,946,427 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
yokunjon | 0 | 13,366,946,427 | 100% |
I thank you for your contribution. Here are my thoughts. Note that, my thoughts are my personal ideas on your post and they are not directly related to the review and scoring unlike the answers I gave in the questionnaire; * **Structure** * It's hard to figure out which part does what. Structuring your post with names of the topic would solve it -e.g, "Storing Cookies", "Retrieving Cookies". I advise you to do so as it would increase the readability of the post. Other than this, the structure of your post looks good. * **Language** * Some of your sentences are artificial. This makes them hard to read and understand. To overcome that, I advise you to check the structure of your sentences. For example, using "we" for each sentence in the post makes it boring to read. * There are some sentences which are hard to understand because of the vocabulary and grammar. I advise you to proofread your posts before posting. It can increase the efficiency of your post. * **Content** * The content in your post is relatively simple. I personally appreciate tutorials which address complex matters more. Nonetheless, it is useful for beginners and I still appreciate it! :) ---- Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category. To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/2-1-2-1-1-4-3-4-). ---- Need help? Write a ticket on https://support.utopian.io/. Chat with us on [Discord](https://discord.gg/uTyJkNm). [[utopian-moderator]](https://join.utopian.io/)
author | yokunjon |
---|---|
permlink | re-duskiharahap-web-development-with-python-5-store-cookie-and-get-cookie-in-template-1543593496200-20181204t231557340z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/2-1-2-1-1-4-3-4-","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"} |
created | 2018-12-04 23:16:00 |
last_update | 2018-12-04 23:16:00 |
depth | 1 |
children | 1 |
last_payout | 2018-12-11 23:16:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 5.916 HBD |
curator_payout_value | 1.906 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,693 |
author_reputation | 19,266,807,595,513 |
root_title | "Web development with python #4 : Store cookie and Get cookie in template" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 76,369,443 |
net_rshares | 12,796,492,038,506 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
codingdefined | 0 | 7,764,281,958 | 7.5% | ||
utopian-io | 0 | 12,603,285,966,749 | 8.96% | ||
emrebeyler | 0 | 113,760,419 | 0.01% | ||
amosbastian | 0 | 36,314,082,594 | 17.1% | ||
organicgardener | 0 | 7,574,872,067 | 25% | ||
reazuliqbal | 0 | 5,382,733,106 | 5% | ||
hakancelik | 0 | 28,547,762,063 | 50% | ||
statsexpert | 0 | 7,919,258,026 | 100% | ||
duski.harahap | 0 | 15,088,483,766 | 100% | ||
mightypanda | 0 | 81,931,299,069 | 60% | ||
fastandcurious | 0 | 1,955,222,199 | 70% | ||
largeadultson | 0 | 451,954,064 | 3.33% | ||
yff | 0 | 162,362,426 | 100% |
Thank you for your review, @yokunjon! Keep up the good work!
author | utopian-io |
---|---|
permlink | re-re-duskiharahap-web-development-with-python-5-store-cookie-and-get-cookie-in-template-1543593496200-20181204t231557340z-20181207t052340z |
category | utopian-io |
json_metadata | "{"app": "beem/0.20.9"}" |
created | 2018-12-07 05:23:42 |
last_update | 2018-12-07 05:23:42 |
depth | 2 |
children | 0 |
last_payout | 2018-12-14 05:23:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 60 |
author_reputation | 152,955,367,999,756 |
root_title | "Web development with python #4 : Store cookie and Get cookie in template" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 76,482,701 |
net_rshares | 13,640,664,646 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
yokunjon | 0 | 13,640,664,646 | 100% |