login form
This commit is contained in:
parent
a1fa2715ad
commit
e3bc41b9f5
|
@ -1,4 +1,6 @@
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
app.config.from_object('config')
|
||||||
|
|
||||||
from app import views
|
from app import views
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
from flask.ext.wtf import Form, TextField, BooleanField
|
||||||
|
from flask.ext.wtf import Required
|
||||||
|
|
||||||
|
class LoginForm(Form):
|
||||||
|
openid = TextField('openid', validators = [Required()])
|
||||||
|
remember_me = BooleanField('remember_me', default = False)
|
||||||
|
|
|
@ -9,6 +9,15 @@
|
||||||
<body>
|
<body>
|
||||||
<div>Microblog: <a href="/index">Home</a></div>
|
<div>Microblog: <a href="/index">Home</a></div>
|
||||||
<hr>
|
<hr>
|
||||||
|
{% with messages = get_flashed_messages() %}
|
||||||
|
{% if messages %}
|
||||||
|
<ul>
|
||||||
|
{% for message in messages %}
|
||||||
|
<li>{{ message }} </li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% endif %}
|
||||||
|
{% endwith %}
|
||||||
{% block content %}{% endblock %}
|
{% block content %}{% endblock %}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -1,4 +1,6 @@
|
||||||
|
<!-- extend base layout -->
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Hi, {{user.nickname}}!</h1>
|
<h1>Hi, {{user.nickname}}!</h1>
|
||||||
{% for post in posts %}
|
{% for post in posts %}
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
<!-- extend base layout -->
|
||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<script type="text/javascript">
|
||||||
|
function set_openid(openid, pr)
|
||||||
|
{
|
||||||
|
u = openid.search('<username>')
|
||||||
|
if (u != -1) {
|
||||||
|
// openid requires username
|
||||||
|
user = prompt('Enter your ' + pr + ' username:')
|
||||||
|
openid = openid.substr(0, u) + user
|
||||||
|
}
|
||||||
|
form = document.forms['login'];
|
||||||
|
form.elements['openid'].value = openid
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<h1>Sign In</h1>
|
||||||
|
<form action="" method="post" name="login">
|
||||||
|
{{form.hidden_tag()}}
|
||||||
|
<p>
|
||||||
|
Please enter your OpenID, or select one of the providers below:<br>
|
||||||
|
{{form.openid(size=80)}}
|
||||||
|
{% for error in form.errors.openid %}
|
||||||
|
<span style="color: red;">[{{error}}]</span>
|
||||||
|
{% endfor %}<br>
|
||||||
|
|{% for pr in providers %}
|
||||||
|
<a href="javascript:set_openid('{{pr.url}}', '{{pr.name}}');">{{pr.name}}</a> |
|
||||||
|
{% endfor %}
|
||||||
|
</p>
|
||||||
|
<p>{{form.remember_me}} Remember Me</p>
|
||||||
|
<p><input type="submit" value="Sign In"></p>
|
||||||
|
</form>
|
||||||
|
{% endblock %}
|
16
app/views.py
16
app/views.py
|
@ -1,5 +1,6 @@
|
||||||
from flask import render_template
|
from flask import render_template, flash, redirect
|
||||||
from app import app
|
from app import app
|
||||||
|
from forms import LoginForm
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
@app.route('/index')
|
@app.route('/index')
|
||||||
|
@ -15,7 +16,18 @@ def index():
|
||||||
'body': 'The Avengers movie was so cool!'
|
'body': 'The Avengers movie was so cool!'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
return render_template("index.html",
|
return render_template('index.html',
|
||||||
title = 'Home',
|
title = 'Home',
|
||||||
user = user,
|
user = user,
|
||||||
posts = posts)
|
posts = posts)
|
||||||
|
|
||||||
|
@app.route('/login', methods = ['GET', 'POST'])
|
||||||
|
def login():
|
||||||
|
form = LoginForm()
|
||||||
|
if form.validate_on_submit():
|
||||||
|
flash('Login requested for OpenID="' + form.openid.data + '", remember_me=' + str(form.remember_me.data))
|
||||||
|
return redirect('/index')
|
||||||
|
return render_template('login.html',
|
||||||
|
title = 'Sign In',
|
||||||
|
form = form,
|
||||||
|
providers = app.config['OPENID_PROVIDERS'])
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
CSRF_ENABLED = True
|
||||||
|
SECRET_KEY = 'you-will-never-guess'
|
||||||
|
|
||||||
|
OPENID_PROVIDERS = [
|
||||||
|
{ 'name': 'Google', 'url': 'https://www.google.com/accounts/o8/id' },
|
||||||
|
{ 'name': 'Yahoo', 'url': 'https://me.yahoo.com' },
|
||||||
|
{ 'name': 'AOL', 'url': 'http://openid.aol.com/<username>' },
|
||||||
|
{ 'name': 'Flickr', 'url': 'http://www.flickr.com/<username>' },
|
||||||
|
{ 'name': 'MyOpenID', 'url': 'https://www.myopenid.com' }]
|
Loading…
Reference in New Issue