Flask Tutorial - “Attribute Error”
Pergunta
I am a newbie - just reached the end of Learn Python the Hard Way and am now trying my hand at Flask. I started with the official tutorial at the Flask website, but am getting stuck at this step:
from __future__ import with_statement
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from contextlib import closing
DATABASE = 'tmp/flaskr.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS',silent=True)
def connect_db():
return sqlite3.connect(app.config['DATABASE'])
def init_db():
with closing(connect_db()) as db:
with app.open.resource('schema.sql') as f:
db.cursor.executescript(f.read())
db.commit()
if __name__ == '__main__':
app.run()
At the Python Shell, if I do
from flaskr import init_db
init_db()
I see: Attribute Error: Flask object has no attribute 'open' (in the line containing app.open.resource in init_db)
. How do i fix this?
Solução
Your code:
app.open.resource
The example code:
app.open_resource
Can you spot the difference? In case you can't, the example code uses an underscore where you use a dot. A Flask object has no attribute called open
, but it does have a method called open_resource
.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow