V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
knowaeap
V2EX  ›  问与答

按着 flask 教程一步一步的走,报错..求指导...

  •  
  •   knowaeap · 2014-04-29 22:20:01 +08:00 · 3307 次点击
    这是一个创建于 3891 天前的主题,其中的信息可能已经有所发展或是发生改变。
    教程地址:http://docs.jinkan.org/docs/flask/tutorial/dbinit.html#tutorial-dbinit

    2 from contextlib import closing
    3 import sqlite3
    4 from flask import Flask, request, session, g, redirect, url_for, \
    5 abort, render_template, flash
    6 # configuration
    7 DATABASE = '/tmp/flaskr.db'
    8 DEBUG = True
    9 SECRET_KEY = 'development key'
    10 USERNAME = 'admin'
    11 PASSWORD = 'default'
    12 # create our little application :)
    13 app = Flask(__name__)
    14 app.config.from_envvar('FLASKR_SETTINGS', silent=True)
    15 def connect_db():
    16 return sqlite3.connect(app.config['DATABASE'])
    17 def init_db():
    18 with closing(connect_db()) as db:
    19 with app.open_resource('schema.sql') as f:
    20 db.cursor().executescript(f.read())
    21 db.commit()
    22 if __name__ == '__main__':
    23 app.run()
    -----------------------------
    报错:
    >>> from flaskr import init_db
    >>> init_db()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "flaskr.py", line 18, in init_db
    with closing(connect_db()) as db:
    File "flaskr.py", line 16, in connect_db
    return sqlite3.connect(app.config['DATABASE'])
    KeyError: 'DATABASE'
    3 条回复    2014-04-30 20:22:45 +08:00
    casparchen
        1
    casparchen  
       2014-04-29 23:30:06 +08:00   ❤️ 1
    replace the following line:
    app.config.from_envvar('FLASKR_SETTINGS', silent=True)
    to:
    app.config.from_object(__name__)
    Because you write all code in one single file. I don't know why you use from_envvar.
    knowaeap
        2
    knowaeap  
    OP
       2014-04-30 20:22:03 +08:00
    thank you very much ... I use from_envvar because I don't know what's meaning...
    knowaeap
        3
    knowaeap  
    OP
       2014-04-30 20:22:45 +08:00
    @casparchen thank you very much ... I use from_envvar because I don't know what's meaning...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1068 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:33 · PVG 03:33 · LAX 11:33 · JFK 14:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.