3

我的 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' }
]

视图.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'])

教程住在这里

谢谢您的帮助。


我的回溯:

    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'

登录.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 %}

提前致谢

4

2 回答 2

3

问题在下面一行:

providers=app.config['OPENID_PROVIDERS'])

app.config 字典不存在键“OPENID_PROVIDERS”。自从您从文件 config.py 加载配置后,您是否还执行了以下操作:

app.config.from_object('config')

在此处阅读更多文档:

http://flask.pocoo.org/docs/config/

于 2013-01-30T19:42:05.490 回答
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'
于 2017-09-04T13:16:14.040 回答