Stuck with key error from flask tutorial

My 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'])

      

The tutorial lives here

Thanks for the help.


My trace:

    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 %}

      

Thank you in advance

+3


source to share


2 answers


The problem is on the line below:

providers=app.config['OPENID_PROVIDERS'])

      

Key "OPENID_PROVIDERS" does not exist for dictionary app.config. You also did the following, since you are loading the config from the config.py file:



app.config.from_object('config')

      

Read more here:

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

+3


source


You need to add the following to views.py :

from flask import flash, redirect
from app import app
from .forms import LoginForm

      



and config.py

CSRF_ENABLED = True
SECRET_KEY = 'your-secret-key'

      

0


source







All Articles