1. home route 설정

# vim app001/routes.py

# ...

# http://host:5001/home - this will be the home page, only accessible for loggedin users
@app.route('/home')
def home():
    # Check if user is loggedin
    if 'loggedin' in session:
        # User is loggedin show them the home page
        return render_template('home.html', username=session['username'])
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))

 

 

 

2. logout route 설정

# vim app001/routes.py

# ...

# http://host:5001/logout - this will be the logout page
@app.route('/logout')
def logout():
    # Remove session data, this will log the user out
   session.pop('loggedin', None)
   session.pop('id', None)
   session.pop('username', None)
   # Redirect to login page
   return redirect(url_for('login'))

 

 

 

3. layout.html 템플릿 작성

여기에서 profile 버튼 클릭시 profile 페이지로 넘어가도록 추후에 수정할 예정입니다. 일단은 home으로 설정했습니다.

<!-- 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('home') }}"><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. home.html 템플릿 작성

<!-- vim app001/templates/home.html -->

{% extends 'layout.html' %}

{% block title %}Home{% endblock %}

{% block content %}
<h2>Home Page</h2>
<p>Welcome back, {{ username }}!</p>
{% endblock %}

 

 

 

5. login 성공시 return 부분 변경

로그인 성공시, return 내용 변경 -> home 화면 불러오기로!

# vim app001/routes.py

# ...

def login():
    
    # ...

        if account:
            session['loggedin'] = True
            session['id'] = account['id']
            session['username'] = account['username']
            #return 'Logged in successfully!'
            return redirect(url_for('home'))
        else:
            msg = 'Incorrect username/password!'
    # ...
    
# ...

 

 

 

6. 웹서버 올리고 로그인, 로그아웃 해보기

$ python3 runserver.py

 

 

 

+ Recent posts