我的config.py:
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.myoopenid.com' }
]
Views.py:
@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'])
教程存在here
感谢您的帮助。
我的追溯:
Traceback (most recent call last):
File "/Users/biofobico/sites/env/microblog/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/biofobico/sites/env/microblog/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/biofobico/sites/env/microblog/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/Users/biofobico/sites/env/microblog/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/biofobico/sites/env/microblog/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/biofobico/sites/env/microblog/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/biofobico/Sites/env/microblog/app/views.py", line 32, in login
providers=app.config['OPENID_PROVIDERS'])
KeyError: 'OPENID_PROVIDERS'
的login.html:
{% extends "base.html" %}
{% block content %}
<script type="text/javascript">
function set_openid(openid, pr)
{
u = openid.search('<username>')
if (u != -1) {
// openid requires a 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:<br>
{{ form.openid(size=80) }}<br>
{% for error in form.errors.openid %}
<span style="color: red;">[{{error}}]</span>
{% endfor %}
|{% 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" name="" value="Sign In"></p>
</form>
{% endblock content %}
提前致谢
答案 0 :(得分:3)
问题在下面一行:
providers=app.config['OPENID_PROVIDERS'])
app.config字典不存在“OPENID_PROVIDERS”键。由于您从config.py文件加载配置,您是否也完成了以下操作:
app.config.from_object('config')
在这里阅读更多文档:
答案 1 :(得分:0)
您需要添加以下 views.py :
from flask import flash, redirect
from app import app
from .forms import LoginForm
并在 config.py
中CSRF_ENABLED = True
SECRET_KEY = 'your-secret-key'