#### Repository https://github.com/python #### What Will I Learn? - Session in flask - Login and Logout system #### 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 In this tutorial, we will continue the previous tutorial related to data storage on our web application. In the previous [tutorial](https://steemit.com/utopian-io/@duski.harahap/web-development-with-python-5-store-cookie-and-get-cookie-in-template-1543593496200). We learned how to save data using *cookie*. The data that we store on cookies are only stored on our client's computer. so, of course, the data we store is not on the server side. Well, in this tutorial we will store data on the server side. we will implement it in the login system. ### Session in Flask to store data on the server side we will use the session. we use sessions to store sensitive data and those that require security, and we will also prevent data from being manipulated by the user. in this tutorial, we will implement the session on the login system. - **Create a login system** We will start using *sessions* on our login system. so later we will save the session user who has successfully logged in. the session that we will use to detect whether the user has logged in or not. to use a session we need to import and we have to make a ***secret key*** like this: **secret_key:** ```app.secret_key = '34ujeifaj21mkll'``` ***Secret key*** is random and determined by ourselves. so you can use a combination of *numbers, letters, punctuation* in **string** form. The secret key is used so that other people cannot manipulate our session, we can use the secret key in the``` app```. <br> - **Save session** We have made the secret key, then we will save the session we will use. before you use the session we must import it first. We can save the session like the following: **import session** ``` from flask session ``` ``` @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']) session['email'] = request.form['email'] return resp return render_template('login.html') ``` We can save a session with an array type like this ```session['email']```, the **'email'** is the key. ```request.form['email']``` comes from the input post. <br> - **Redirect with session** We have learned how to save sessions, now we will learn how to use the session to be used as a login system. so later we will redirect when the user is not logged in. Before we use the redirect function we must import it first. for more details, we can see the code below: **import redirect** ``` from flask redirect ``` **Route Login** ``` @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']) session['email'] = request.form['email'] return resp if 'email' in session: email = session['email'] return redirect(url_for('profileFunc', email = email)) return render_template('login.html') ``` - Well we will use the ```session ['email']``` to detect users who are logged in. We can check whether the email session is in session this way ```if 'email' in session```. - If the email session exists, then we will redirect to ```/profile/<email>```. We will redirect by passing the session value. to redirect we can use the ```redirect ()```.in the ```riderect ()``` we can use the url_for function to direct the URL to the destination. We can redirect the URL by calling the function used in the URL. like the following example ```redirect(url_for('profileFunc', email = email))```. The following is routing using the ```profileFunc()```: **Route profile** ``` @app.route('/profile/<email>') def profileFunc(email): return 'Welcome to my tutorial %s' %email ``` In this routing, we will receive the email parameter and we will use it to check whether the data we redirect has been successful. to see a discussion about URL parameters you can follow the following [tutorial](https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-3-get-method-query-parameter-and-navigate-routing-1543411189705).After all steps have been completed, We can see the results as shown below:  as we saw in the picture above, now we can detect whether the user is logged in or not. If the user is logged in, we will return the user to ```return 'Welcome to my tutorial %s' %email``` and the parameters that we have passed can we appear ```milleaduski@gmail.com```.  <br> - **Redirect to another page** Now I will redirect users with different pages according to what we want, You can change its function like the following: **Route Profile** ``` @app.route('/profile/<email>') def profileFunc(email): return render_template('profile.html', email = email) ``` **profile.html** ``` <!DOCTYPE html> <html> <head> <title>Template Jinja2</title> </head> <body> <h1>This is the profile page</h1> <h2 style="color:blue;">Hai my name is {{email}}</h1> <a href="{{url_for('logout')}}">Logout</a> </body> </html> ``` Now we will be redirected to the profile page like the picture below:  ### Logout and delete session Now we need to create a system log out on the web application that we have learned. for that, we need to create a new routing task to log out the user who is logged in, so we can delete the session delete by accessing the route . for more details, we can look at the code below: ``` @app.route('/logout') def logout(): session.pop('email', None) return redirect(url_for("login")) ``` - ```/logout``` is the routing used to log out and the function is ```logout``` - to delete the session we are using we can use the **pop** function from a **session** like this ```session.pop()```. We can specify which session we will delete by passing it as the first parameter ```'email'``` and the second parameter we value is **none** and then we can redirect with ```url_for()```. <br> - **Add logout button** In the frontend section, we need to add an interface so that the user can log out by clicking the button. Here is the frontend code: **profile.html** ``` <!DOCTYPE html> <html> <head> <title>Template Jinja2</title> </head> <body> <h1>This is the profile page</h1> <h2 style="color:blue;">Hai my name is {{email}}</h1> <a href="{{url_for('logout')}}">Logout</a> </body> </html> ``` We can also use ```url_for()``` in the frontend section by access the logout function like this ``` <a href="{{url_for('logout')}}">Logout</a>```. After all stages are complete we can run our program and see the results as shown below:  W can see in the picture above we have created a login system using the session contained on the server. so the session that we made only lasted about 2 hours. we have also learned how to redirect and check sessions to give access to routing that we will use. I hope you can develop this web application. That is all from me. thank you #### Curriculum [Web development 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) [Web development with python #4: Store cookie and Get cookie in template](https://steemit.com/utopian-io/@duski.harahap/web-development-with-python-5-store-cookie-and-get-cookie-in-template-1543593496200) #### Proof of work done https://github.com/milleaduski/python-web-app
author | duski.harahap | ||||||
---|---|---|---|---|---|---|---|
permlink | web-development-with-python-5-session-in-flask-and-login-and-logout-system-1544025770372 | ||||||
category | utopian-io | ||||||
json_metadata | {"app":"steemit/0.1","format":"markdown","image":["https://ipfs.busy.org/ipfs/QmeuDAu942Ta2BRbLRYTyRzej6ThErJD8YFB5kWCmciqQt","https://ipfs.busy.org/ipfs/QmXbXpQg9w3yDojUV7ScfMAmdS4KWMhdWJ1ZjDqBCkmKZu","https://ipfs.busy.org/ipfs/QmY83n3sysWEYBofUddtWxv4EB8392VdqZiD5Gf6ZLXQ7K","https://ipfs.busy.org/ipfs/QmXqxwbvzYeEhgM5n2hTogKLmF5Q8B7x3aSikdCrKYXFTw"],"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-development-with-python-5-store-cookie-and-get-cookie-in-template-1543593496200","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-1-flask-initialization-and-routing-system-1542726589553","https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-2-templating-jinja2-and-method-post-on-routing-system-1542987551736","https://github.com/milleaduski/python-web-app"]} | ||||||
created | 2018-12-05 16:02:54 | ||||||
last_update | 2018-12-06 02:47:09 | ||||||
depth | 0 | ||||||
children | 3 | ||||||
last_payout | 2018-12-12 16:02:54 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 12.368 HBD | ||||||
curator_payout_value | 3.993 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 8,624 | ||||||
author_reputation | 60,094,717,098,672 | ||||||
root_title | "Web development with python #5 : Session in flask and Login and Logout system" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 100,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 76,405,015 | ||||||
net_rshares | 28,168,964,645,437 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
arcange | 0 | 35,462,307,617 | 4% | ||
raphaelle | 0 | 2,145,079,860 | 4% | ||
penguinpablo | 0 | 114,791,156,989 | 5% | ||
eforucom | 0 | 20,423,223,481 | 1% | ||
elviento | 0 | 902,345,692 | 2.22% | ||
miniature-tiger | 0 | 99,955,923,366 | 50% | ||
aleister | 0 | 8,264,594,407 | 16% | ||
jga | 0 | 1,663,749,920 | 10.36% | ||
yehey | 0 | 6,267,895,427 | 10% | ||
mercadosaway | 0 | 1,406,706,303 | 100% | ||
ninjas | 0 | 618,339,318 | 100% | ||
codingdefined | 0 | 7,772,216,745 | 7.5% | ||
bachuslib | 0 | 20,187,650,882 | 100% | ||
steemitri | 0 | 137,233,414,606 | 100% | ||
leir | 0 | 1,890,356,912 | 50% | ||
mcfarhat | 0 | 10,883,082,186 | 12.13% | ||
martusamak | 0 | 2,718,690,093 | 25% | ||
pataty69 | 0 | 8,837,756,449 | 25% | ||
tixinhacapitinha | 0 | 5,219,285,474 | 25% | ||
utopian-io | 0 | 26,830,034,624,388 | 20.72% | ||
shammi | 0 | 87,512,677,520 | 90% | ||
jaff8 | 0 | 43,298,041,041 | 30.34% | ||
gattino | 0 | 1,243,502,029 | 8% | ||
scipio | 0 | 59,531,258,134 | 25% | ||
greenorange | 0 | 548,538,173 | 100% | ||
cryptonized | 0 | 16,013,526,349 | 5% | ||
steemitag | 0 | 3,313,049,447 | 10% | ||
dedicatedguy | 0 | 164,480,024,692 | 100% | ||
amosbastian | 0 | 64,145,761,230 | 30.34% | ||
asaj | 0 | 6,006,942,757 | 30.34% | ||
viperblckz | 0 | 4,111,499,380 | 100% | ||
sudefteri | 0 | 4,160,348,934 | 100% | ||
micaelacf | 0 | 938,856,027 | 25% | ||
properfraction | 0 | 746,514,514 | 100% | ||
hakancelik | 0 | 27,257,136,920 | 50% | ||
xinvista | 0 | 182,317,796 | 100% | ||
ezravandi | 0 | 458,627,561 | 2.29% | ||
enlighted | 0 | 79,644,396 | 25% | ||
fel1xw | 0 | 1,024,059,864 | 50% | ||
steemsig | 0 | 71,677,056 | 50% | ||
s6476721 | 0 | 482,733,814 | 100% | ||
glitterbot | 0 | 316,614,955 | 50% | ||
boulangerefinger | 0 | 471,817,622 | 100% | ||
goote | 0 | 482,345,859 | 100% | ||
pointingevasive | 0 | 471,564,473 | 100% | ||
whitenathan | 0 | 492,563,239 | 100% | ||
dpoll.economy | 0 | 71,612,709 | 50% | ||
kacmorova | 0 | 105,087,659 | 100% | ||
clusonsopi | 0 | 509,851,999 | 100% | ||
triburultrac | 0 | 510,894,678 | 100% | ||
olalmagta | 0 | 523,907,653 | 100% | ||
nuclearbooby | 0 | 482,997,073 | 100% | ||
luc.real | 0 | 214,300,441 | 100% | ||
nieloagranca | 0 | 6,535,697,081 | 8% | ||
jasmineg47 | 0 | 504,397,483 | 100% | ||
steemchoose | 0 | 68,862,338,548 | 3.5% | ||
ibericovar | 0 | 472,221,292 | 100% | ||
duarte9sousa | 0 | 2,682,485,956 | 2.5% | ||
spotted | 0 | 1,756,554,019 | 40% | ||
bhaski | 0 | 2,017,333,877 | 25% | ||
minnowsmith | 0 | 1,099,838,943 | 40% | ||
zeniththrash | 0 | 483,455,752 | 100% | ||
keymiphosla | 0 | 468,390,930 | 100% | ||
glownortolor | 0 | 533,331,825 | 100% | ||
smartcurator | 0 | 658,333,053 | 50% | ||
emily02 | 0 | 536,820,364 | 100% | ||
curbot | 0 | 2,240,240,683 | 100% | ||
meulybnqcv | 0 | 491,921,945 | 100% | ||
bluesniper | 0 | 67,658,331,436 | 4% | ||
delabo | 0 | 100,918,833,488 | 50% | ||
chrnerd | 0 | 553,916,705 | 100% | ||
ctime | 0 | 103,551,505,948 | 4% |
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-session-in-flask-and-login-and-logout-system-1544025770372-20181208t020452z |
category | utopian-io |
json_metadata | "{"app": "beem/0.20.9"}" |
created | 2018-12-08 02:04:54 |
last_update | 2018-12-08 02:04:54 |
depth | 1 |
children | 0 |
last_payout | 2018-12-15 02:04:54 |
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 #5 : Session in flask and Login and Logout system" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 76,524,270 |
net_rshares | 0 |
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** * I appreciate your improvement in the structure of the post. It looks better in my opinion. Thanks! * **Language** * I personally think the usage of the first person reduces the efficiency of the post. I advise you to consider using less first person. Still, this is a personal thought, don't get me wrong! :) * 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** * As I stated in my latest review, 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-session-in-flask-and-login-and-logout-system-1544025770372-20181207t072450177z |
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-07 07:24:51 |
last_update | 2018-12-07 07:24:51 |
depth | 1 |
children | 1 |
last_payout | 2018-12-14 07:24:51 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 5.840 HBD |
curator_payout_value | 1.873 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,498 |
author_reputation | 19,266,807,595,513 |
root_title | "Web development with python #5 : Session in flask and Login and Logout system" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 76,486,245 |
net_rshares | 12,684,897,933,891 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
yuxi | 0 | 31,489,248,187 | 100% | ||
codingdefined | 0 | 8,063,284,141 | 7.5% | ||
utopian-io | 0 | 12,498,223,232,999 | 8.81% | ||
emrebeyler | 0 | 121,169,277 | 0.01% | ||
amosbastian | 0 | 39,800,442,708 | 19.17% | ||
organicgardener | 0 | 7,299,141,856 | 25% | ||
reazuliqbal | 0 | 5,544,206,969 | 5% | ||
mightypanda | 0 | 89,494,202,179 | 60% | ||
fastandcurious | 0 | 1,967,038,115 | 60% | ||
largeadultson | 0 | 452,725,947 | 3.33% | ||
linknotfound | 0 | 1,720,100,377 | 100% | ||
monster-inc | 0 | 598,577,496 | 100% | ||
yff | 0 | 124,563,640 | 100% |
Thank you for your review, @yokunjon! Keep up the good work!
author | utopian-io |
---|---|
permlink | re-re-duskiharahap-web-development-with-python-5-session-in-flask-and-login-and-logout-system-1544025770372-20181207t072450177z-20181209t120243z |
category | utopian-io |
json_metadata | "{"app": "beem/0.20.9"}" |
created | 2018-12-09 12:02:45 |
last_update | 2018-12-09 12:02:45 |
depth | 2 |
children | 0 |
last_payout | 2018-12-16 12:02:45 |
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 #5 : Session in flask and Login and Logout system" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 76,584,578 |
net_rshares | 14,145,948,720 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
yokunjon | 0 | 14,145,948,720 | 100% |