< 폴더 경로 >
< app.py >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
from flask import Flask, flash, redirect, render_template, request, url_for
app = Flask(__name__)
app.secret_key = 'random string'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login', methods = ['GET', 'POST'])
def login():
error = None #에러값 없음.
if request.method == 'POST':
if request.form['username'] != 'admin' or \
request.form['password'] != 'admin':
error = 'Invalid username or password. Please try again!' #에러값 저장.
else:
flash('You were successfully logged in')
return redirect(url_for('index'))
return render_template('login.html', error = error)
if __name__ == "__main__":
app.run(host='0.0.0.0', port = 80, debug = True)
|
secret_key는 java의 UUID 클래스와 같은 개념으로 사용된다.
컴퓨터 서버 안에서 실행되는 프로그램들이 다수 일 때 이를 구분할 목적과
프로그램 보안 목적에서 사용한다.
< index.html >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<!doctype html>
<html>
<head>
<title>Flask Message flashing</title>
</head>
<body>
{% with messages = get_flashed_messages() %} <!--파이썬에 값을 받아서 html에 출력가능.-->
{% if messages %}
<ul>
{% for mes in messages %}
<li>{{ mes }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<h1>Flask Message Flashing Example</h1>
<p>Do you want to <a href = "{{ url_for('login') }}">
<b>log in?</b></a></p>
</body>
</html>
|
< login.html >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<!doctype html>
<html>
<body>
<h1>Login</h1>
{% if error %}
<p><strong>Error:</strong> {{ error }}
{% endif %}
<form action = "" method = post>
<dl>
<dt>Username:</dt>
<dd>
<input type = text name = username
value = "{{request.form.username }}">
</dd>
<dt>Password:</dt>
<dd><input type = password name = password></dd>
</dl>
<p><input type = submit value = Login></p>
</form>
</body>
</html>
|
id와 pw값이 틀려서 에러메시지가 출력됨.
id와 pw값이 같아서 성공메시지가 출력됨.
'PRACTICE > Basic' 카테고리의 다른 글
[Flask] SQLite 예제 (0) | 2020.09.24 |
---|---|
[Flask] File Uploading 예제 (파일 제출 안할 경우 flash로 에러메시지 출력) (0) | 2020.09.24 |
[Flask] Redirect & Errors 예제 (0) | 2020.09.24 |
[Flask] Sessions 예제 (0) | 2020.09.24 |
[Flask] Cookies 예제 (0) | 2020.09.24 |
댓글