python - 请问以下Flask数据库配置哪里出错了呢?我是根据教程一步一步来做的:
ringa_lee
ringa_lee 2017-04-17 12:03:02
0
4
527

请问以下Flask数据库配置哪里出错了呢?我是根据教程一步一步来做的:
教程: http://dormousehole.readthedocs.org/en/latest/tutorial/dbinit.html#tutorial-dbinit

#------code------
# all the imports
import sqlite3
from flask import Flask, request, session, g, redirect, url_for,abort, render_template, flash
from contextlib import closing

# configuration
DATABASE = '/tmp/flaskr.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'
​
# create our little application :)
app = Flask(__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', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()

#------Runing in Python IDEL------
>>> from flaskr import init_db
>>> init_db()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "flaskr.py", line 22, in init_db
    with closing(connect_db()) as db:
  File "flaskr.py", line 19, in connect_db
    return sqlite3.connect(app.config['DATABASE'])
KeyError: 'DATABASE'
ringa_lee
ringa_lee

ringa_lee

reply all(4)
小葫芦

DATABASE = '/tmp/flaskr.db' should be no problem.
Add this line to the file:

    app.config.from_object(__name__)

That’s it. This line of code will load the configuration from this file.

左手右手慢动作

DATABASE = '/tmp/flaskr.db' This line of configuration, confirm that the database can be found after running? If so, do you need to confirm whether the path is relative or absolute? ?

PHPzhong

sudo python

阿神

1. Configuration is not a separate file
2. DATABASE uses absolute paths

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template