-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_with_errorhandlers.py
More file actions
67 lines (59 loc) · 1.95 KB
/
session_with_errorhandlers.py
File metadata and controls
67 lines (59 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import Flask, request, url_for, redirect, render_template,make_response,session,escape
app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
@app.errorhandler(404) # This handler is used to avoid errors on the page
def page_not_found(error):
return redirect(url_for('index'))
@app.route('/')
def index():
if 'username' in session:
return 'Logged in as %s' % escape(session['username'])
return 'You are not logged in'
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
session['username'] = request.form['username']
return redirect(url_for('index'))
return '''
<form method="post">
<p><input type=text name=username>
<p><input type=submit value=Login>
</form>
'''
@app.route('/logout')
def logout():
# remove the username from the session if it's there
session.pop('username', None)
return redirect(url_for('index'))
# @app.errorhandler(404) # This handler is used to avoid errors on the page
# def page_not_found(error):
# return render_template('hello.html')
# @app.errorhandler(404)
# def not_found(error):
# resp = make_response(render_template('hello.html'), 404)
# resp.headers['X-Something'] = 'A value'
# return resp
#
# @app.route('/ss')
# def input_input():
# resp = make_response(render_template('input.html'))
# resp.set_cookie('finput', 'user_value1')
# resp.set_cookie('linput', 'user_value2')
# return resp
#
# @app.route('/add', methods=['POST'])
# def user_input():
# user1 = int(request.form['finput'])
# user2 = int(request.form['linput'])
# user1 = user1 + user2
# return redirect(url_for('result_display', name = user1))
#
# @app.route('/add/<name>')
# def result_display(name):
# return "Result: {}".format(name)
#
# def user_input():
# user1 = r
# if __name__=="__main__":
# # app.config['DEBUG'] = True
# app.run()