1. 로그인 화면에서의 register 버튼 활성화
<a href="{{ url_for('register') }}">Register</a>
위 라인을 추가하여 아래와 같이 변경합니다.
<!-- vim app001/templates/index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
</head>
<body>
<div class="login">
<h1>Login</h1>
<div class="links">
<a href="{{ url_for('login') }}" class="active">Login</a>
<a href="{{ url_for('register') }}">Register</a>
</div>
<form action="{{ url_for('login') }}" method="post">
<label for="username">
<i class="fas fa-user"></i>
</label>
<input type="text" name="username" placeholder="Username" id="username" required>
<label for="password">
<i class="fas fa-lock"></i>
</label>
<input type="password" name="password" placeholder="Password" id="password" required>
<div class="msg">{{ msg }}</div>
<input type="submit" value="Login">
</form>
</div>
</body>
</html>
2. register route 추가
# vim app001/routes.py
# ...
# http://host:5005/register - this will be register page
@app.route('/register', methods=['GET', 'POST'])
def register():
msg = 'Creating User Page'
# If already loggedin, redirect to home
if 'loggedin' in session:
return redirect(url_for('home'))
if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form:
username = request.form['username']
password = request.form['password']
email = request.form['email']
username_already_exist = User.check_username_exist(username)
email_already_exist = User.check_email_exist(email)
if username_already_exist:
msg = 'That username is already exist'
elif email_already_exist:
msg = 'That email is already exist'
else:
User.useradd(username, password, email)
msg = 'Create User Success!'
return redirect(url_for('login'))
return render_template('register.html', msg=msg)
# ...
3. register web page 추가
<!-- vim app001/templates/register.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Register</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
</head>
<body>
<div class="login">
<h1>Register</h1>
<div class="links">
<a href="{{ url_for('login') }}">Login</a>
<a href="{{ url_for('register') }}" class="active">Register</a>
</div>
<form action="{{ url_for('register') }}" method="post">
<label for="username">
<i class="fas fa-user"></i>
</label>
<input type="text" name="username" placeholder="Username" id="username" required>
<label for="password">
<i class="fas fa-lock"></i>
</label>
<input type="password" name="password" placeholder="Password" id="password" required>
<label for="email">
<i class="fas fa-user"></i>
</label>
<input type="email" name="email" placeholder="Email" id="email" required>
<div class="msg">{{ msg }}</div>
<input type="submit" value="Register my user">
</form>
</div>
</body>
</html>
'Development > Flask with Python' 카테고리의 다른 글
[Flask1.1][Mysql] 실습 - 9 : jwt 활용하여 인증 기능 추가하기 (0) | 2020.01.17 |
---|---|
[Flask1.1][Mysql] 실습 - 8 : 비밀번호 암호화하기 및 비밀번호check with bcrypt (0) | 2020.01.17 |
[Flask1.1][Mysql] 실습 - 6 : 로그인시 접속한 기기의 IP 정보 받기 & 로그인 페이지 리다이렉트 기능 추가 (0) | 2020.01.06 |
[Flask1.1][Mysql] 실습 - 5 : routes와 models 파일 별도로 만들기 (0) | 2020.01.06 |
[Flask1.1][Mysql] 실습 - 4 : 서버 Port 변경하기 (0) | 2020.01.06 |