V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yanest
V2EX  ›  Python

照着 Flask 官方的文档写的代码,运行出错,请各位老师帮忙看看

  •  
  •   yanest · 2014-08-03 13:39:33 +08:00 · 4938 次点击
    这是一个创建于 3765 天前的主题,其中的信息可能已经有所发展或是发生改变。
    http://dormousehole.readthedocs.org/en/latest/tutorial/dbinit.html#tutorial-dbinit
    照着这个弄的。
    初始化数据库出错,初学者不明白问题出在哪,请帮忙指点一下

    代码如下:
    >>> 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'
    >>>

    # flaskr.py:

    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 out 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()

    if __name__ == '__main__':
    app.run()
    6 条回复    2016-01-11 01:12:54 +08:00
    cloverstd
        1
    cloverstd  
       2014-08-03 13:53:32 +08:00
    你有 export FLASKR_SETTINGS=/path/to/settings.cfg 吗
    yanest
        2
    yanest  
    OP
       2014-08-03 14:14:51 +08:00
    @cloverstd 没有啊,文档李没说需要这么干啊
    notedit
        3
    notedit  
       2014-08-03 14:55:15 +08:00
    你把app.config 打印出来就知道了 出错的意思是没有 DATABASE

    没看到你设置 FLASKR_SETTINGS 当然会出错
    notedit
        4
    notedit  
       2014-08-03 15:01:04 +08:00
    """
    Simply define the environment variable FLASKR_SETTINGS that points to a config file to be loaded. The silent switch just tells Flask to not complain if no such environment key is set.

    """
    yanest
        5
    yanest  
    OP
       2014-08-03 15:34:43 +08:00
    @notedit 好像有点懂了,我改一下试试。
    改成app.config.from_object(__name__) 就可以了吧
    LokiStone
        6
    LokiStone  
       2016-01-11 01:12:54 +08:00
    @yanest 我就是按着教程来的,可是改成 app.config.from_object(__name__)还是不行啊,还是报数据库错误
    File "/Users/mac/PycharmProjects/flaskr/flaskr.py", line 21, in connect_db
    return sqlite3.connect(app.config['DATABASE'])
    KeyError: 'DATABASE'
    请问您最后是怎嘛解决这个问题啊,拜托了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2788 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 12:56 · PVG 20:56 · LAX 04:56 · JFK 07:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.