运行第八章个人博客报错 SQLAlchemy not such table: post

,看上面说是没有post这个表,但是在models中是定义了这个表啊。

把你完整的执行(命令)流程发出来一下,记得纯文本。我猜是没有执行flask initdb:upside_down_face:

顺便说这个错误信息没必要截图。

1 个赞

请参考论坛说明了解如何正确的发帖。另外,第 8 章开始介绍了如何运行实例程序。

我是在Pycharm中运行的,提示出这个错误,在pycharm中运行我直接是把所需要的包安装了下,然后配置了下运行环境。第八章开头说运行这个程序使用
&flask forge
&flask run
可是我的会报错啊,可是命名flask forge这个命令注册过了啊。

你执行哪条命令出现这个报错的?麻烦发一下完整的错误回溯信息,用文本的形式。

1 个赞

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: post
[SQL: SELECT post.id AS post_id, post.title AS post_title, post.body AS post_body, post.timestamp AS post_timestamp, post.can_comment AS post_can_comment, post.category_id AS post_category_id
FROM post ORDER BY post.timestamp DESC
LIMIT ? OFFSET ?]
[parameters: (10, 0)]
(Background on this error at: http://sqlalche.me/e/e3q8)
这是完整的报错信息,并没有执行什么特殊的命令啊,直接就是在Pycharm中运行的,配置了运行环境并且安装了需要的包然后运行的,出现的这个报错。

数据库需要手动创建并生成表。在运行前需要在 PyCharm 终端窗口输入 flask initdb 或 flask forge 命令。如果你执行这两个命令报错,那就贴出来完整的错误输出。

另外,确保你已经安装了依赖 Faker。

我在Pycharm中运行了flask initdb,出现了如下错误,faker包我已经安装了。
Traceback (most recent call last):
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
2309, in call
return self.wsgi_app(environ, start_response)
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
2295, in wsgi_app
response = self.handle_exception(e)
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
1741, in handle_exception
reraise(exc_type, exc_value, tb)
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask_compat.py”, l
ine 35, in reraise
raise value
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
2292, in wsgi_app
response = self.full_dispatch_request()
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask_compat.py”, l
ine 35, in reraise
raise value
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
1813, in full_dispatch_request
rv = self.dispatch_request()
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask_debugtoolbar_
init_.py”, line 125, in dispatch_request
return view_func(**req.view_args)
File “D:\bluelog\bluelog\blueprints\blog.py”, line 26, in index
return render_template(‘blog/index.html’, pagination=pagination, posts=posts)
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\templating.py”
, line 135, in render_template
context, ctx.app)
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\templating.py”
, line 117, in _render
rv = template.render(context)
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\jinja2\asyncsupport.
py”, line 76, in render
return original_render(self, *args, **kwargs)
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\jinja2\environment.p
y”, line 1008, in render
return self.environment.handle_exception(exc_info, True)
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\jinja2\environment.p
y”, line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\jinja2_compat.py”,
line 37, in reraise
raise value.with_traceback(tb)
File “D:\bluelog\bluelog\templates\blog\index.html”, line 2, in top-level template code
{% from ‘bootstrap/pagination.html’ import render_pager %}
File “D:\bluelog\bluelog\templates\base.html”, line 1, in top-level template code
{% from ‘bootstrap/nav.html’ import render_nav_item %}
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\templating.py”
, line 58, in get_source
return self._get_source_fast(environment, template)
File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\templating.py”
, line 86, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: bootstrap/nav.html
127.0.0.1 - - [19/Mar/2019 10:09:40] “GET /?debugger=yes&cmd=resource&f=style.css HTTP/1.1”
200 -
127.0.0.1 - - [19/Mar/2019 10:09:40] “GET /?debugger=yes&cmd=resource&f=jquery.js HTTP/1.1”
200 -
127.0.0.1 - - [19/Mar/2019 10:09:40] “GET /?debugger=yes&cmd=resource&f=debugger.js HTTP/1.
1” 200 -
127.0.0.1 - - [19/Mar/2019 10:09:40] "GET /?debugger=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1
" 200 -
127.0.0.1 - - [19/Mar/2019 10:09:40] “GET /?debugger=yes&cmd=resource&f=console.png HTTP/1.
1” 200 -

  • To enable the debugger you need to enter the security pin:
  • Debugger pin code: 806-707-780
    127.0.0.1 - - [19/Mar/2019 10:11:22] “GET /?debugger=yes&cmd=printpin&s=k9z730ykG8Llo70NXjS
    p HTTP/1.1” 200 -
    127.0.0.1 - - [19/Mar/2019 10:11:26] “GET /?debugger=yes&cmd=pinauth&pin=&s=k9z730ykG8Llo70
    NXjSp HTTP/1.1” 200 -
    127.0.0.1 - - [19/Mar/2019 10:11:31] “GET / HTTP/1.1” 500 -
    Traceback (most recent call last):
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
    2309, in call
    return self.wsgi_app(environ, start_response)
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
    2295, in wsgi_app
    response = self.handle_exception(e)
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
    1741, in handle_exception
    reraise(exc_type, exc_value, tb)
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask_compat.py”, l
    ine 35, in reraise
    raise value
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
    2292, in wsgi_app
    response = self.full_dispatch_request()
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
    1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
    1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask_compat.py”, l
    ine 35, in reraise
    raise value
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\app.py”, line
    1813, in full_dispatch_request
    rv = self.dispatch_request()
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask_debugtoolbar_
    init_.py”, line 125, in dispatch_request
    return view_func(**req.view_args)
    File “D:\bluelog\bluelog\blueprints\blog.py”, line 26, in index
    return render_template(‘blog/index.html’, pagination=pagination, posts=posts)
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\templating.py”
    , line 135, in render_template
    context, ctx.app)
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\templating.py”
    , line 117, in _render
    rv = template.render(context)
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\jinja2\asyncsupport.
    py”, line 76, in render
    return original_render(self, *args, **kwargs)
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\jinja2\environment.p
    y”, line 1008, in render
    return self.environment.handle_exception(exc_info, True)
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\jinja2\environment.p
    y”, line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\jinja2_compat.py”,
    line 37, in reraise
    raise value.with_traceback(tb)
    File “D:\bluelog\bluelog\templates\blog\index.html”, line 2, in top-level template code
    {% from ‘bootstrap/pagination.html’ import render_pager %}
    File “D:\bluelog\bluelog\templates\base.html”, line 1, in top-level template code
    {% from ‘bootstrap/nav.html’ import render_nav_item %}
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\templating.py”
    , line 58, in get_source
    return self._get_source_fast(environment, template)
    File “c:\users\lenovo.virtualenvs\helloflask-hutz0icq\lib\site-packages\flask\templating.py”
    , line 86, in _get_source_fast
    raise TemplateNotFound(template)
    jinja2.exceptions.TemplateNotFound: bootstrap/nav.html
    127.0.0.1 - - [19/Mar/2019 10:11:31] “GET /?debugger=yes&cmd=resource&f=style.css HTTP/1.1”
    200 -
    127.0.0.1 - - [19/Mar/2019 10:11:31] “GET /?debugger=yes&cmd=resource&f=jquery.js HTTP/1.1”
    200 -
    127.0.0.1 - - [19/Mar/2019 10:11:31] “GET /?debugger=yes&cmd=resource&f=debugger.js HTTP/1.
    1” 200 -
    127.0.0.1 - - [19/Mar/2019 10:11:31] "GET /?debugger=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1
    " 200 -
    127.0.0.1 - - [19/Mar/2019 10:11:31] “GET /?debugger=yes&cmd=resource&f=console.png HTTP/1
    .1” 200 -
    127.0.0.1 - - [19/Mar/2019 10:11:32] “GET /?debugger=yes&cmd=resource&f=console.png HTTP/1
    .1” 200 -

错误信息已经很明显了:

jinja2.exceptions.TemplateNotFound: bootstrap/nav.html

表明模板 bootstrap/nav.html 没有找到,说明你没有安装 Bootstrap-Flask,或是错误的安装了 Flask-Bootstrap。