是否有一个“如果命令"?确定我的sqlalchemy sqlite查询命令是否为null? [英] Is there an "if command" to identify if my sqlalchemy sqlite query command is null?

查看:41
本文介绍了是否有一个“如果命令"?确定我的sqlalchemy sqlite查询命令是否为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有这些代码:

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语句" Python/Sqlalchemy代码来识别我的查询命令是否找不到任何信息,然后显示上面的引用.类似这样的东西:

It's an sqlalchemy query command and it let users on my website to search/filter and query blogs. But sometimes, the input they give is not in the database and my website will not show anything. So, instead of not showing anything, I want to show a text says: "Couldn't find relating things to your search". I wonder if there is any "if statement" Python/Sqlalchemy codes I can use to identify if my query command didn't find any information and then to show the quote above. Something like:

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

与many_posts连接的部分HTML代码:

Part of my HTML codes that connect with many_posts:

    <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>

我是编码的初学者,如果您能帮助我,我将不胜感激.谢谢!

I'm a beginner in coding and I would greatly appreciate if you could help me. Thank you!

=======================答案/解决方案=========================

我使用以下代码:

方法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中,我使用:

In HTML, I use:

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

推荐答案

不确定,但是您可以尝试两件事:

Not sure, but you might try two things:

如果未返回任何行,则查询结果可能不会评估为 False .一个空列表的结果为 False ,但是查询结果可能是其他类型的对象,并且可能不会为False.转换为列表可能会有所帮助.

Perhaps the result of a query doesn't evaluate to False if no row is returned. an empty list evaluates to False, but query results are probably objects of a different type and might not evaluate to False. converting to a list might help.

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

用于调试目的.您可能已经添加了以下内容.打印/记录对理解很有帮助

for debugging purposes. you could have added following. Printing / logging is quite helpful to understand

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()是不可迭代的.(您是否尝试过阅读分页文档.它可能会提到是否有关于结果数量的信息)

OK the output of SqlAlchemy paginate() is not iterable. (Did you try to read the doc of paginate. It might mention whether there is some info about amount of results)

以下可能适用于SqlAlchemy(我没有时间尝试)

Following might work with SqlAlchemy (I don't have time to try)

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)

这篇关于是否有一个“如果命令"?确定我的sqlalchemy sqlite查询命令是否为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆