Sqlalchemy Flask中的查询未返回行
因此,我试图根据用户搜索的关键字在Sqlalchemy Flask中的查询未返回行,flask,sqlalchemy,Flask,Sqlalchemy,因此,我试图根据用户搜索的关键字在heroku中查询数据库,但我遇到了一个问题,它没有返回行 当我使用下面的查询时,它给出了正确的行 下面这一行给出了一个错误,或未定义,但我看不出问题所在 使用以下查询时,不会返回任何行: 下面是我的search()函数的代码 @app.route("/search", methods=['GET', 'POST']) @login_required def search(): keyword = request.form
heroku
中查询数据库,但我遇到了一个问题,它没有返回行
- 当我使用下面的查询时,它给出了正确的行
- 下面这一行给出了一个错误,
,但我看不出问题所在或未定义
- 使用以下查询时,不会返回任何行:
search()函数的代码
@app.route("/search", methods=['GET', 'POST'])
@login_required
def search():
keyword = request.form.get("keyword")
books = Book.query.filter(Book.title.like("%keyword%")).all()
#books is null, if condition is not satisfied
if books is None:
return render_template("home.html")
return render_template('search.html', title='Search', books=books)
models.py
from flaskblog import db, login_manager
from flask_login import UserMixin
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
class User(db.Model, UserMixin):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
class Book(db.Model):
__tablename__ = "books"
id = db.Column(db.Integer, primary_key=True)
isbn = db.Column(db.String, nullable=False, unique=True)
title = db.Column(db.String, nullable=False)
author = db.Column(db.String, nullable=False)
year = db.Column(db.Integer, nullable=False)
search.html:
{% extends "layout.html" %}
{% block content %}
<div class="col-md-12">
<!-- Search form -->
<form method="post" action="search" class="form-inline d-flex justify-content-center md-form form-sm">
<input class="form-control form-control-sm mr-3 w-75" type="text" name="keyword" placeholder="Search" aria-label="Search">
<i class="fas fa-search" aria-hidden="true"></i>
</form>
</div>
{% if books %}
<div class="row">
<div class="col-sm-8 blog-main">
{% for book in books %}
<div class="blog-post">
<h2 class="blog-post-title"> {{ book.title }}</h2>
<p>
{{ book.isbn }}
</p>
</div>
{% endfor %}
</div>
</div>
{% endif %}
{% endblock content %}
{%extends“layout.html”%}
{%block content%}
{%if书籍%}
{书籍中书籍的百分比%}
{{book.title}}
{{book.isbn}
{%endfor%}
{%endif%}
{%endblock内容%}
- 下面的查询将返回
书籍
的标题中包含单词/字符串关键字
要将关键字
用作变量和非单词/字符串,请使用python插值或f-string:
Book.query.filter( Book.title.like('%{}%'.format(keyword)) ).all()
or
Book.query.filter( Book.title.like(f'%{keyword}%') ).all()
- 要使用需要导入的
或
(和
,)表达式,请参阅此
- 在
search.html中
搜索表单通常使用GET
方法提交
<!-- Search form -->
<form method="GET" action="search" class="form-inline d-flex justify-content-center md-form form-sm">
<input class="form-control form-control-sm mr-3 w-75" type="text" name="keyword" placeholder="Search" aria-label="Search">
<i class="fas fa-search" aria-hidden="true"></i>
</form>
from markupsafe import escape # to escape user input and prevent sql injection ..
@app.route("/search")
@login_required
def search():
keyword = escape(request.form.get("keyword")) # here
books = Book.query.filter(Book.title.like(f'%{keyword}%')).all() # here
# books is null, if condition is not satisfied
# if books is None:
# return render_template("home.html")
# return "books" object even it's empty and in your temple check it and display
# the right message according to it
return render_template('search.html', title='Search', books=books)
- 在
search.html中
{%if书籍%}
{书籍中书籍的百分比%}
{{book.title}}
{{book.isbn}
{%endfor%}
{%else%}
没有包含“关键字”的书籍
{%endif%}
SQLAlchemy的或
函数需要导入,例如从SQLAlchemy导入或
导入。你把它导入你的模块了吗?@Snakecherb不,我没有。现在它不会给出错误,但也不会返回行。这些行确实存在于db中&与它的连接很好,所以我还必须导入escape from flask。它现在工作得很好,tyvm!
Book.query.filter( Book.title.like("%keyword%") ).all()
Book.query.filter( Book.title.like('%{}%'.format(keyword)) ).all()
or
Book.query.filter( Book.title.like(f'%{keyword}%') ).all()
from sqlalchemy import or_
books = Book.query.filter( or_(Book.isbn.like(f'%{keyword}%'), Book.title.like(f'%{keyword}%') )
<!-- Search form -->
<form method="GET" action="search" class="form-inline d-flex justify-content-center md-form form-sm">
<input class="form-control form-control-sm mr-3 w-75" type="text" name="keyword" placeholder="Search" aria-label="Search">
<i class="fas fa-search" aria-hidden="true"></i>
</form>
from markupsafe import escape # to escape user input and prevent sql injection ..
@app.route("/search")
@login_required
def search():
keyword = escape(request.form.get("keyword")) # here
books = Book.query.filter(Book.title.like(f'%{keyword}%')).all() # here
# books is null, if condition is not satisfied
# if books is None:
# return render_template("home.html")
# return "books" object even it's empty and in your temple check it and display
# the right message according to it
return render_template('search.html', title='Search', books=books)
{% if books %}
<div class="row">
<div class="col-sm-8 blog-main">
{% for book in books %}
<div class="blog-post">
<h2 class="blog-post-title"> {{ book.title }}</h2>
<p>
{{ book.isbn }}
</p>
</div>
{% endfor %}
</div>
</div>
{% else %}
no books with "keyword"
{% endif %}