from flask import Flask, render_template, url_for, redirect, request, session
app = Flask(__name__)
app.secret_key = 'loginner'
users = {
'admin' : 'secret',
'foo' : 'myfoo',
}
@app.get("/")
def main():
return render_template('main.html')
@app.get('/login')
def login_form():
return render_template('login.html')
@app.post('/login')
def login():
username = request.form.get('username')
password = request.form.get('password')
if username and password and username in users and users[username] == password:
session['username'] = username
return redirect(url_for('account'))
return render_template('login.html', error_message="Invalid login")
@app.get("/account")
def account():
username = session.get('username')
if not username:
return redirect(url_for('login'))
return render_template('account.html', username=username)
@app.get('/logout')
def logout():
if not session.get('username'):
return render_template('message.html', message="Not logged in")
else:
del session['username']
return render_template('logout.html')
{% include 'header.html' %}
Account information for {{username}}
<div>
<a href="/">home</a> | <a href="/login">login</a> | <a href="/logout">logout</a> | <a href="/account">account</a>
</div>
{% include 'header.html' %}
Home page
{% include 'header.html' %}
<form method="POST">
<input name="username" placeholder="username">
<input name="password" placeholder="password" type="password">
<input type="submit" value="Login">
</form>
{% if error_message %}
<h2>{{error_message}}</h2>
{% endif %}
{% include 'header.html' %}
Bye bye
{% include 'header.html' %}
Home
{% include 'header.html' %}
{{message}}
import app
def test_app():
web = app.app.test_client()
rv = web.get('/')
assert rv.status == '200 OK'
assert b'Home' in rv.data
# TODO: add more tests