python - Web10
python flask10
html2
>>이어진 내용:
1. POST방식 전송
POST방식이 GET방식과 다르게 가지는 가장 큰 차이점음 url에 전송하는 데이터가 보이지 않는 다는 점이다. 데이터 크기에 제한이 없어 많은 데이터를 처리할 수 있지만 GET방식보다는 속도가 느리다.(보조 자료: http://soul0.tistory.com/185)
2. POST방식으로 데이터 얻기(.py)
GET방식과 POST방식 모두를 통해 데이터를 얻는 코드
@app.route('/login', methods=['GET', 'POST'])
def login():
if(request.method =='GET'):
return render_template('login2.html')
else:
uid = request.form.get('uid')
upw = request.form.get('upw')
print(uid ,"and", upw)
if(not uid) or (not upw):
return render_template('error.html', msg ='비정상 접근')
if uid =='n' and upw=='1':
return redirect(url_for('mainService'))
else:
return render_template('error.html', msg='아이디 비번 확인')
@app.route('/main')
def mainService():
return 'main service'
@app.route('/login', methods=['GET', 'POST'])
:route인자에 methods를 통해 전송할 방식을 선택한다. 현재는 GET과 POST를 모두 사용하기 때문에 list를 이용해 두 방식을 모두 포함했다.
if(request.method =='GET'):
return render_template('login2.html')
uid = request.form.get('uid')
upw = request.form.get('upw')
f(not uid) or (not upw):
return render_template('error.html', msg ='비정상 접근')
if uid =='n' and upw=='1':
return redirect(url_for('mainService'))
else:
return render_template('error.html', msg='아이디 비번 확인')
@app.route('/main')
def mainService():
return 'main service'
3.POST방식으로 데이터 얻기(.html)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h3>로그인1111</h3>
<form action = "/login", method="POST">
<input type='text' name='uid' placeholder="ID" required autofocus/> <br>
<input type='password' name='upw' placeholder="PASSWORD" required/> <br>
<input type='submit' value="로그인"/>
</form>
</body>
</html>
<form action = "/login", method="POST">
:action인자의 값을 '/login'으로 설정해 이 태그가 얻은 데이터를 전송할 주소(서버의 스크립트 파일)을 정해준다. method는 POST방식으로 정했다.
<input type='text' name='uid' placeholder="ID" required autofocus/>
:input태크는 사용자가 입력할 수 있는 공간을 만들어준다. type을 통해 어떤 방식으로 입력받을 지를 결정한다. type='text'는 문자열을 입력하는 공간을 만든다. name을 통해 현재 태그의 이름을 정한다. placeholder는 문자열을 입력할 공간이 옅게 써진 글씨를 설정한다. required를 사용하면 해당 태그에 입력을 하지 않을 경우 다른 행위를 하지 못하게 막는다. autofocus는 미리 커서가 있을 위치를 설정한다.
<input type='password' name='upw' placeholder="PASSWORD" required/>
:type="password"로 지정해 비밀 번호를 입력할 수 있는 공간을 만든다.
<input type='submit' value="로그인"/>
:type='submit'은 인터페이스는 버튼 모양의 인터페이를 가진다. 이 버튼을 누르면 form 을 서버에 전송해주는 역할을 한다.4. 자바스크립트와 jinja2를 이용해 경고창 띄우기
<script>
alert('{{ msg }}');
{% if url : %}
document.location.href="{{url}}";
{% else : %}
history.back();
{% endif %}
</script>
alert('{{ msg }}');
:alert는 경고창을 띄우는 함수다. {{msg}}는 jinja2를 사용한 부분이다. jinja2는 javascript나 html에 삽입할 수 있다. {{변수}}를 통해 인자로 전달받은 변수를 사용할 수 있다.
{% if url : %}
document.location.href="{{url}}";
{% else : %}
history.back();
{% endif %}
전체코드
1 .py
from flask import Flask, request, render_template, redirect, url_for
app = Flask(__name__)
@app.route('/')
def home():
return 'hello world3'
@app.route('/login', methods=['GET', 'POST'])
def login():
if(request.method =='GET'):
return render_template('login2.html')
else:
uid = request.form.get('uid')
upw = request.form.get('upw')
print(uid ,"and", upw)
if(not uid) or (not upw):
return render_template('error.html', msg ='비정상 접근')
if uid =='n' and upw=='1':
return redirect(url_for('mainService'))
else:
return render_template('error.html', msg='아이디 비번 확인')
@app.route('/main')
def mainService():
return 'main service'
if __name__ == '__main__':
app.run(debug=True)
2 login2.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h3>로그인1111</h3>
<form action = "/login", method="POST">
<input type='text' name='uid' placeholder="ID" required autofocus/> <br>
<input type='password' name='upw' placeholder="PASSWORD" required/> <br>
<input type='submit' value="로그인"/>
</form>
</body>
</html>
3.error.html
<script>
alert('{{ msg }}');
{% if url : %}
document.location.href="{{url}}";
{% else : %}
history.back();
{% endif %}
</script>
<script>
alert('{{ msg }}');
{% if url : %}
document.location.href="{{url}}";
{% else : %}
history.back();
{% endif %}
</script>
실행화면
1. http://127.0.0.1:5000/2. http://127.0.0.1:5000/login
로그인이 실패했을 경우
3. http://127.0.0.1:5000/main
(로그인이 성공한-uid==n and upw==1-)
댓글
댓글 쓰기