1. 프로필 route 설정
# vim app001/routes.py
# ...
# http://host:5001/profile - this will be the profile page, only accessible for loggedin users
@app.route('/profile')
def profile():
# Check if user is loggedin
if 'loggedin' in session:
# We need all the account info for the user so we can display it on the profile page
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM accounts WHERE id = %s', [session['id']])
account = cursor.fetchone()
# Show the profile page with account info
return render_template('profile.html', account=account)
# User is not loggedin redirect to login page
return redirect(url_for('login'))
2. 프로필 페이지 작성
<!-- vim app001/templates/profile.html -->
{% extends 'layout.html' %}
{% block title %}Profile{% endblock %}
{% block content %}
<h2>Profile Page</h2>
<div>
<p>Your account details are below:</p>
<table>
<tr>
<td>Username:</td>
<td>{{ account['username'] }}</td>
</tr>
<tr>
<td>Password:</td>
<td>{{ account['password'] }}</td>
</tr>
<tr>
<td>Email:</td>
<td>{{ account['email'] }}</td>
</tr>
</table>
</div>
{% endblock %}
3. layout 페이지의 profile 버튼 정상화
이전 실습 게시물에서의 Profile 버튼 링크를 임시로 home 향하게 해두었으니, 이제 home에서 profile로 변경
( body 태그 내부에 있는 url_for : home -> profile 변경 )
<!-- vim app001/templates/layout.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %}{% endblock %}</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 class="loggedin">
<nav class="navtop">
<div>
<h1>Website Title</h1>
<a href="{{ url_for('home') }}"><i class="fas fa-home"></i>Home</a>
<a href="{{ url_for('profile') }}"><i class="fas fa-user-circle"></i>Profile</a>
<a href="{{ url_for('logout') }}"><i class="fas fa-sign-out-alt"></i>Logout</a>
</div>
</nav>
<div class="content">
{% block content %}{% endblock %}
</div>
</body>
</html>
4. 웹서버 올리고 로그인, 프로필 확인하기
$ python3 runserver.py
'Development > Flask with Python' 카테고리의 다른 글
[Flask1.1][Mysql] 실습 - 4 : 서버 Port 변경하기 (0) | 2020.01.06 |
---|---|
[Flask1.1] client의 IP 정보 가져오기 (0) | 2020.01.05 |
[Flask1.1][Mysql] 실습 - 2 : 홈 화면 추가 및 로그아웃 기능 구현 (0) | 2020.01.02 |
[Flask1.1][Mysql] 실습 - 1 : Flask와 Mysql 연동하여 로그인 기능 구현하기 (0) | 2019.12.29 |
[Solved][Centos7] ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output. (0) | 2019.12.29 |