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

 

 

 

+ Recent posts