Last active
February 13, 2017 15:34
-
-
Save ariarijp/1a8db6ed098984bdc30854b113adfad1 to your computer and use it in GitHub Desktop.
Flaskの簡単なログインサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from flask import Flask, flash, redirect, render_template, request, session, abort, url_for | |
app = Flask(__name__) | |
app.secret_key = 'VERY_VERY_SECRET' | |
app.config['SESSION_TYPE'] = 'filesystem' | |
@app.route('/') | |
def home(): | |
return render_template('login.html') | |
@app.route('/login', methods=['POST']) | |
def do_admin_login(): | |
if request.form['password'] == 'p@ssw0rd' and request.form['username'] == 'admin': | |
session['logged_in'] = True | |
else: | |
flash('Wrong password!') | |
return home() | |
@app.route('/logout') | |
def do_admin_logout(): | |
session.pop('logged_in', None) | |
return redirect(url_for('home')) | |
if __name__ == "__main__": | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>Flask Login Example</h1> | |
{% block body %} | |
{% with messages = get_flashed_messages() %} | |
{% if messages %} | |
<div class="alert alert-info"> | |
{% for message in messages %} | |
<p>{{ message }}</p> | |
{% endfor %} | |
</div> | |
{% endif %} | |
{% endwith %} | |
{% if session['logged_in'] %} | |
<div> | |
<p>ログイン中です!</p> | |
<a class="btn btn-primary btn-block" href="/logout">ログアウト</a> | |
</div> | |
{% else %} | |
<form action="/login" method="POST"> | |
<div class="form-group"> | |
<input type="username" class="form-control" name="username" placeholder="ユーザー名(admin)"> | |
</div> | |
<div class="form-group"> | |
<input type="password" class="form-control" name="password" placeholder="パスワード(p@ssw0rd)"> | |
</div> | |
<div class="form-group"> | |
<input type="submit" class="form-control btn btn-primary" value="ログイン"> | |
</div> | |
</form> | |
{% endif %} | |
{% endblock %} | |
</div> | |
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
appdirs==1.4.0 | |
click==6.7 | |
Flask==0.12 | |
itsdangerous==0.24 | |
Jinja2==2.9.5 | |
MarkupSafe==0.23 | |
packaging==16.8 | |
pyparsing==2.1.10 | |
six==1.10.0 | |
Werkzeug==0.11.15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
login.htmlはtemplatesの中に置く