首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在flask web应用程序中未显示sqlite查询的结果

问在flask web应用程序中未显示sqlite查询的结果
EN

Stack Overflow用户
提问于 2019-02-24 00:52:57
回答 1查看 302关注 0票数 0

我正在尝试学习flask,所以决定遵循这个教程:

https://www.blog.pythonlibrary.org/2017/12/14/flask-101-adding-editing-and-displaying-data/

我刚刚用下面的代码更新了我的main函数:

代码语言:javascript
复制
@app.route('/results')
def search_results(search):
    results = []
    search_string = search.data['search']

    if search.data['search'] == '':
        qry = db_session.query(Album)
        results = qry.all()

    if not results:
        flash('No results found!')
        return redirect('/')
    else:
        # display results
        table = Results(results)
        table.border = True
        return render_template('results.html', table=table)

但是,当我将一个专辑添加到数据库中,并尝试使用搜索选项将其查询回来时,它显示没有结果。DB文件是正确创建的,到目前为止,我的代码与教程中的完全相同。

我所做的唯一更改是从表中添加导入结果。下面是完整的main.py。你能给我一些关于在哪里寻找罪犯的指导吗?就像我说的,只是学习,所以任何建议以一种友好的方式重新资源将非常感谢(初学者程序员)。

代码语言:javascript
复制
from app import app
from db_setup import init_db, db_session
from forms import MusicSearchForm, AlbumForm
from flask import flash, render_template, request, redirect
from models import Album, Artist
from tables import Results 

init_db()


def save_changes(album, form, new=False):
    """
    Save the changes to the database
    """
    # Get data from form and assign it to the correct attributes
    # of the SQLAlchemy table object
    artist = Artist()
    artist.name = form.artist.data

    album.artist = artist
    album.title = form.title.data
    album.release_date = form.release_date.data
    album.publisher = form.publisher.data
    album.media_type = form.media_type.data

    if new:
        # Add the new album to the database
        db_session.add(album)

    # commit the data to the database
    db_session.commit()


@app.route('/', methods=['GET', 'POST'])
def index():
    search = MusicSearchForm(request.form)
    if request.method == 'POST':
        return search_results(search)

    return render_template('index.html', form=search)


@app.route('/results')
def search_results(search):
    results = []
    search_string = search.data['search']

    if search.data['search'] == '':
        qry = db_session.query(Album)
        results = qry.all()

    if not results:
        flash('No results found!')
        return redirect('/')
    else:
        # display results
        table = Results(results)
        table.border = True
        return render_template('results.html', table=table)


@app.route('/new_album', methods=['GET', 'POST'])
def new_album():
    """
    Add a new album
    """
    form = AlbumForm(request.form)

    if request.method == 'POST' and form.validate():
        # save the album
        album = Album()
        save_changes(album, form, new=True)
        flash('Album created successfully!')
        return redirect('/')

    return render_template('new_album.html', form=form)


if __name__ == '__main__':
    app.run()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-24 03:24:35

毫无疑问,您已经在源代码中使用了大量print()语句,但没有发现任何有启发性的东西。在这里,DB模型中的缓存行可能是难以理解的方面,记录sqlite调用将有助于了解这一点。

使用以下命令:

代码语言:javascript
复制
import logging

logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)

它很嘈杂,但它会显示行何时命中后端数据库以及何时被检索。

养成像这样反复发出调试查询的习惯,这样您就可以确定保存了什么:

代码语言:javascript
复制
$ echo 'select * from album;' | sqlite3 music.db

对于可重复测试,可以方便地将数据库文件复制到备份位置,然后在每次测试运行之前将冻结的快照cp到活动文件的顶部。重要的是,在复制后重新启动正在运行的flask应用程序。设置FLASK_DEBUG=1可以帮助你做到这一点。

另外,使用SQLAlchemyDebugPanel的jeverling suggests。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54843843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档