是否有一个“if命令”来标识我的sqlalchemy sqlite查询命令是否为null?

ws51t4hk  于 2021-08-09  发布在  Java
关注(0)|答案(3)|浏览(352)

我有以下代码:

many_posts = BlogPost.query.filter(or_((BlogPost.problem_name.ilike("%" + form.search.data + "%")),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc()).paginate(page=page, per_page=10)

return render_template('blog_search_result.html', id_list=id_list, many_posts=many_posts, no_posts=no_posts)

这是一个sqlalchemy查询命令,它允许我的网站上的用户搜索/过滤和查询博客。但有时,他们提供的输入不在数据库中,我的网站也不会显示任何内容。所以,我不想显示任何内容,而是想显示一个文本,上面写着:“找不到与您的搜索相关的内容”。我想知道是否有任何“if statement”python/sqlalchemy代码可以用来确定查询命令是否没有找到任何信息,然后显示上面的引号。比如:

if many_posts is null:
            no_posts= "Couldn't find relating problems with your search"

我的html代码中有一部分与许多帖子相连接:

<h4>Search results<span class="iconify" data-icon="icons8:search" data-inline="false"></span>:</h4>
    <p>{{ no_posts }}</p>

<div class="container row row-cols-1 row-cols-md-2 text-center">
{% for post in many_posts.items%}

    <div class="card border-dark mb-3 " style="width: 20rem;">
     <div class="card-body ">
         <h7><a class="text-warning" href="{{ url_for('users.user_posts', username=post.creator.first_name+post.creator.middle_name+post.creator.last_name) }}"><img class="text-center rounded" src="{{ url_for('static', filename='profile_pics/'+ post.creator.profile_image) }}" width = "35" height = "35" alt=""> {{ post.creator.first_name}} {{ post.creator.middle_name }} {{ post.creator.last_name }}</a></h7>
         <p></p>
         <img class="text-center rounded responsive1" alt="" src="{{ url_for('static', filename='blog_pics/'+ post.blog_image) }}" width = "495" height = "250">
         {# Need caution for post.blog_image on the code above #}
         <p></p>
         <h2><a class="card-tittle text-body problem" href="{{ url_for('blog_posts.blog_view', blog_validated_id=post.blog_id) }}">{{ post.problem_name[0:40]}}..</a></h2>
         <p class="card-text">{{ post.text[0:100] }}</p>
         <p><small class="text-muted">Posted on: {{ post.date.strftime('%Y-%m-%d') }}</small></p>
         <a class="btn btn-warning" href="{{ url_for('blog_posts.blog_view', blog_validated_id=post.blog_id) }}">Read more</a>
     </div>

我是一个初学者在编码,我将非常感谢如果你能帮助我。谢谢您!
============================答案/解决方案=========================
我使用以下代码:
方法1:

many_posts0 = BlogPost.query.filter(or_((BlogPost.problem_name.ilike("%" + form.search.data + "%")),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc())

        many_posts = many_posts0.paginate(page=page, per_page=10)
        num_posts = many_posts0.count()
        if num_posts == 0:
            no_posts="Couldn't find relating problems"
        else:
            no_posts=''

return render_template('blog_search_result.html', id_list=id_list, many_posts=many_posts, num_posts=num_posts, no_posts=no_posts)

在html中,我使用:

<p>{{no_posts}}</p>

方法2:

many_posts = BlogPost.query.filter(or_((BlogPost.problem_name.ilike("%" + form.search.data + "%")),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc()) 
no_posts = ''

if not many_posts.first():
   no_posts= "Couldn't find relating problems with your search"
many_posts = many_posts.paginate(page=page, per_page=10)
9rbhqvlz

9rbhqvlz1#

你需要 not 操作员处于if状态:

if not <variable>:
    #block_code

你不需要申报或通过 no_posts 渲染模板。您可以通过只在jinja代码html模板中执行这些操作来实现所需的结果。
福巴.py

posts = BlogPost.query.filter(or_((BlogPost.problem_name.ilike("%" + form.search.data + "%")),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc()).paginate(page=page, per_page=10)

return render_template('blog_search_result.html', id_list=id_list, posts=posts)

博客搜索结果.html

<h4>Search results<span class="iconify" data-icon="icons8:search" data-inline="false"></span>:</h4>
{% if not posts %}
   <p> Couldn't find relating problems with your search </p>
{% else %}
  <div class="container row row-cols-1 row-cols-md-2 text-center">
  {% for post in posts.items %}
    <div class="card border-dark mb-3 " style="width: 20rem;">
     <div class="card-body ">
         <h7><a class="text-warning" href="{{ url_for('users.user_posts', username=post.creator.first_name+post.creator.middle_name+post.creator.last_name) }}"><img class="text-center rounded" src="{{ url_for('static', filename='profile_pics/'+ post.creator.profile_image) }}" width = "35" height = "35" alt=""> {{ post.creator.first_name}} {{ post.creator.middle_name }} {{ post.creator.last_name }}</a></h7>
         <p></p>
         <img class="text-center rounded responsive1" alt="" src="{{ url_for('static', filename='blog_pics/'+ post.blog_image) }}" width = "495" height = "250">
         {# Need caution for post.blog_image on the code above #}
         <p></p>
         <h2><a class="card-tittle text-body problem" href="{{ url_for('blog_posts.blog_view', blog_validated_id=post.blog_id) }}">{{ post.problem_name[0:40]}}..</a></h2>
         <p class="card-text">{{ post.text[0:100] }}</p>
         <p><small class="text-muted">Posted on: {{ post.date.strftime('%Y-%m-%d') }}</small></p>
         <a class="btn btn-warning" href="{{ url_for('blog_posts.blog_view', blog_validated_id=post.blog_id) }}">Read more</a>
     </div>
    </div>
  {% endfor %}
  </div>
{% endif %}
vdzxcuhz

vdzxcuhz2#

不确定,但你可以尝试两件事:
也许查询结果的计算结果不是 False 如果没有返回行。空列表的计算结果为 False ,但查询结果可能是不同类型的对象,其计算结果可能不会为false。转换为列表可能会有所帮助。

return render_template('blog_search_result.html', id_list=id_list, posts=list(posts))

用于调试目的。你可以添加以下内容。打印/记录非常有助于理解

print("Posts is ", posts, "and as bool it returns ", bool(posts))
return render_template('blog_search_result.html', id_list=id_list, posts=list(posts))

第一次反馈后
好的,sqlalchemy的输出 paginate() 是不可忍受的(你有没有读过帕吉纳医生的书。它可能会提到是否有一些关于结果数量的信息)
以下内容可能适用于sqlalchemy(我没有时间尝试)

posts_to_show = BlogPost.query.filter(or_((BlogPost.problem_name.ilike("%" + form.search.data + "%")),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc())

post_page = posts_to_show.paginate(page=page, per_page=10)

num_posts = posts.count()  # there might be something better to find out if there is at least one post, but I don't know it

return render_template('blog_search_result.html', id_list=id_list, posts=posts_page, num_posts=num_posts)
2eafrhcq

2eafrhcq3#

这里有很多方法可以完成你想要完成的事情!您可以检查查询中是否没有这样的数据:

many_posts = BlogPost.query.filter(or_((BlogPost.problem_name.ilike("%" + form.search.data + "%")),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc()) #without paginate
no_posts = None

# check if doesn't have anything inside

if not many_posts.scalar():
   no_posts= "Couldn't find relating problems with your search"
many_posts = many_posts.paginate(page=page, per_page=10)

您可以在这里阅读有关scalar()的更多信息。

相关问题